javascript调用父窗口的函数和变量
发布日期:2008-05-28最近更新:2008-05-28来源:BHCODE作者:
web开发的时候,有时候需要使用其他页面上写好的javasript函数、变量。如弹出窗口需要使用父窗口中的函数,框架1需要使用框架2中的函数。
调用函数、变量的方法一样,都是需要首先获得你需要调用的函数所在的window对象,然后通过window.method()或者 window.variable 来调用。
下面的示例演示了一个弹出窗口如何调用起父窗口中的方法和变量。
父窗口:1.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
var theHelloStr = "Hello all.";
function sayHello()
{
alert("hello, method");
}
function openBtnClicked()
{
var newwin=window.open('2.html',"test","toolbar=no,location=no, ......
1、对象的继承,一般的做法是复制:Object.extend
prototype.js的实现方式是:
Object.extend = function(destination, source){
for (property in source) {
destination[property] = source[property];
}
return destination;
}
除此之外,还有种方法,就是:Function.apply(当然使用Function.call也是可以的)
apply方法能劫持另外一个对象的方法,继承另外一个对象的属性
Function.apply(obj,args)方法能接收两个参数
obj:这个对象将代替Function类里this对象
args:这个是数组,它将作为参数传给Function(args-->arguments) ......
在js中,每个对象都有一个prototype属性:返回对象类型原型的引用。很拗口!习语“依葫芦画瓢”,这里的葫芦就是原型,那么“瓢.prototype” 返回的就是葫芦,或者“瓢.prototype= new 葫芦()”。
prototype的用途:
继承
有一个对象--子类:
function 子类() {
this.lastname = "Samuel";
}
有一个对象--父类:
function 父类() {
this.firstname = "Shen";
}
现在子类是有名无姓,父类是有姓无名,如果子类要有名有姓的话,只要说明--子类的原型是父类--就可以了,即子类继承自父类:
子类.prototype = new 父类();
至此,子类就不再是有名无姓了。
alert(子类.firstname + " " + 子类.lastname); //Samuel Shen
牵一发而动全身
既然prototype返回的是原型的引用,那么如果改变原型的话,所有继承自该原型的对象都将受到影响。
function Point(x,y) {
this.x = x;
this.y = y;
}
var p1 = new Point(1,2);
var p2 = new Point(3,4);
Point.prototype.z = 0; //动态为Point的原型添加了属性
alert(p1.z); //0
alert(p2.z); / ......
JS自带函数
concat
将两个或多个字符的文本组合起来,返回一个新的字符串。
var a = "hello";
var b = ",world";
var c = a.concat(b);
alert(c);
//c = "hello,world"
indexOf
返回字符串中一个子串第一处出现的索引(从左到右搜索)。如果没有匹配项,返回 -1 。
var index1 = a.indexOf("l");
//index1 = 2
var index2 = a.indexOf("l",3);
//index2 = 3
charAt
返回指定位置的字符。
var get_char = a.charAt(0);
//get_char = "h"
lastIndexOf
返回字符串中一个子串最后一处出现的索引(从右到左搜索),如果没有匹配项,返回 -1 。
var index1 = lastIndexOf('l');
//index1 = 3
var index2 = lastIndexOf('l',2)
//index2 = 2
match
检查一个字符串匹配一个正则表达式内容,如果么有匹配返回 null。
var re = new RegExp(/^\w+$/);
var is_alpha1 = a.match(re);
//is_alpha1 = "hello"
var is_alpha2 = b.match(re);
//is_alpha2 = null
substring
返回字符串的一个子串,传入参数是起始位置和结束位置。
var sub_string1 = a.substring(1);
//sub_string1 = "ello"
var sub_string2 = a.substring(1,4);
//sub_string2 = "ell ......
二十、Bookmarklet
1、什么是Bookmarklet?(What's a Bookmarklet?)
Q:什么是Bookmarklet?
A:Bookmarklet是整个都被包含在超链接URL中的一小段JavaScript程序。(JavaScript URL就是这个样子:<a href="javascript:the code goes here">。)多数浏览器允许用户添加这些JavaScript URL书签,就像添加其他超链接一样。bookmarklet这个词是由Steve Kanga创造的,他也自己开发了许多有用的bookmarklet。下面就是一个简单的例子:后退bookmarklet。
Go-Back Bookmarklet
这个bookmarklet等价于浏览器的后退按钮。点击上面的标题可以预览其效果。在标题上点击右键,(根据浏览器)选择添加书签或者添加到收藏夹,就可以把它添加到你的书签中。
2、Bookmarklets:浏览器支持(Bookmarklets: Browser Support)
Q:那些浏览器支持bookmarklets?
A:下列浏览器都支持bookmarklet:
Netscape Navigator 3.0 及更新版本
Internet Explorer 4.0 及更新版本
并不是所有的bookmarklet都可以在任何浏览器中正常工作。 这是因为不同浏览器对JavaScript的实现不同。要编写一个跨浏览器的bookmarklet:
使用所有目标浏览器支持的JavaScript子集。 ......
语法
oNewWindow = window.open( [sURL] [, sName] [, sFeatures] )
sURL 可选. URL 字符串 . 如果URL为空, 将以about:blank打开.
sName 可选. 字符串 描述打开窗口的名字(name). 可以做为form 和 a 标签的TARGET属性值 .
sFeatures 可选. 字符串 格式如"fullscreen=yes,toolbar=yes".channelmode = { yes | no | 1 | 0 } 是否以频道模式打开. 默认值 no.
参数
directories = { yes | no | 1 | 0 } 是否打开连接按钮. 默认值 yes.
fullscreen = { yes | no | 1 | 0 } 是否以全屏方式打开. 默认值 no. ALT+F4 可关闭. 测试中发现如果值为3,5,7....等时,可以显示横向渐变条.
height = number 设置窗口高度;
left = number 窗口离屏幕左方的距离,必须大于0.
location = { yes | no | 1 | 0 } 是否显示连接栏, 默认值 yes.
menubar = { yes | no | 1 | 0 } 是否显示菜单栏. 默认值 yes.
resizable = { yes | no | 1 | 0 } 窗口是否可调整. 默认值 yes.
scrollbars = { yes | no | 1 | 0 } 是否显示滚动条. 默认值 yes.
status = { yes | no | 1 | 0 } 是否显示状态栏. 默认值 yes.
titlebar = { yes | no | 1 | 0 } 是否显示标题栏. 可能被忽略除非 ......