js 获取父 url

JavaScript08

js 获取父 url,第1张

<!DOCTYPE html>

<html>

<head>

<title>子窗口</title>

<script type="text/javascript">

document.write(window.parent.document.URL)

</script>

</head>

<body>

</body>

</html> <!DOCTYPE html>

<html>

<head>

<title>父窗口</title>

</head>

<body>

<-- 需要与子窗口保持在同一目录下,或修改此处连接地址 -->

<iframe src="child.html" style="width:500pxheight:500px"></iframe>

</body>

</html>

这个需要在IE中查看,如果要用谷歌浏览器,需要部署到服务器上。

<script language="JavaScript">

alert(parent.location.href)//父框架地址

alert(parent.location.title)//父框架标题

</script>

当然可以,但是有一个前提条件,那就是a.html,b.html所在的域名必须相同。

举例说明:

有两个页面,A、B。其中B是A中通过iframe的方式引入的嵌入页面。

A页面的地址是:http://www.myexample.com/a.html

B页面的地址是:http://www.myexample.com/b.html

假设页面中只有一个iframe。

那么在A中执行document.getElementsByTagName('iframe')[0].contentWindow就可以取得B中的window对象。既然获得了B的window对象,那A触发一个按钮也就自然能够调用B中的一个函数并在B中执行显示。

另:如果A、B的主域名相同

距离说明:

有两个页面,A、B。其中B是A中通过iframe的方式引入的嵌入页面。

A页面的地址是:http://www.myexample.com/a.html

B页面的地址是:http://bbs.myexample.com/b.html

那么,在A、B中都设置document.domain = 'myexample.com'即可

其余情况同A、B是相同域名的情况