javascript调用父窗口的函数和变量
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,top=100,left=100,directories=no,status=yes,menubar=no,scrollbars=yes,location=no,resizable=yes,width=300,height=200");
newwin.focus();
}
function sayHello2()
{
window.sayHello();
}
</script>
</head>
<body>
<input type="button" name="openBtn" value="打开窗口" onclick="openBtnClicked()" />
</body>
</html>
弹出窗口:2.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">
function btn1Clicked()
{
window.opener.sayHello();
}
function btn2Clicked()
{
var helloMsg = window.opener.theHelloStr;
alert(helloMsg);
}
</script>
</head>
<
相关文档:
javascript:void(0)
刚开始都不知道是啥意思
其实就是一个死链接,什么事情都不做。
<a id="link_${user.account}" href="javascript:void(0);" onclick="changSubmit();" style="cursor:pointer;height:25px;width:60px;margin-top:10px;">发送email</a>
这句 ......
<html>
<head>
<script language="JavaScript">
<!--页面载入时初始化所有未选择的数据-->
function loadAllUnselected()
{
var unassignedTable=doc ......
var obj = new Object();
obj.name = "hello";
obj['name'] = "world";
alert(obj.name);
被人问到这样一段代码,alert的结果应该是什么呢?
我回答:hello #结果大错特错!
应该是“world”!!!
因为js是一种松散类型的语言,obj虽然被声明是一个对象,但是把它当成数组来访问也没什么不可以。
虽然 ......
因为JSON 是 javascript 的一个子集,所以,在javascript 中使用JSON是非常简单的。
js 代码
var myJSONObject = {"bindings": [
{"ircEvent": "PRIVMSG", "method":&n ......
var myDate = new Date();
myDate.getYear(); //获取当前年份(2位)
myDate.getFullYear(); //获取完整的年份(4位,1970-????)
myDate.getMonth(); //获取当前月份(0-11,0代表1月)
myDate.getDate(); ......