js如何跨页面调用函数?

JavaScript013

js如何跨页面调用函数?,第1张

其中parent.html中含有IFrame并且IFrame指向child.html。现在需要在parent.html/child.html中调用child.html/parent.html的一个js方法。

具体的代码实现如下:

parent.html父页面:

代码如下:

<html>

<head>

<script type="text/javascript">

function parent_click(){

alert("来自父页面")

}

</script>

</head>

<body>

<input type="button" value="调用本页面函数" onclick="parent_click()" />

<input type="button" value="调用子页面函数" onclick='window.frames["childPage"].child_click()' />

<iframe id="childPage" name="childPage" src="inner.html" width="100%" frameborder="0"></iframe>

</body>

</html>

child.html子页面:

代码如下:

<html>

<head>

<script type="text/javascript">

function child_click(){

alert("调用的子页面函数")

}

</script>

</head>

<body>

<input type="button" value="调用父页面函数" onclick='parent.window.parent_click()' />

<input type="button" value="调用本页面函数" onclick="child_click()" />

</body>

</html>

可以放在网页的最后面也可以放在你网页原来的JS的地方

2.把JS里的最后一句放到网页里面,代码如下:

window.onload=function(){glide.layerGlide(true,'icon_num_top','show_pic_top',205,2,0.1,'top')}