onclick v no onclick;如何在没有点击的情况下获得结果

时间:2014-09-05 21:16:58

标签: javascript jquery html iframe

我有这个代码,当它们在iframe中更改时成功跟踪网址:

<html>
<body>
<iframe src="start.php"></iframe>
<a href="#" onclick="alert(frames[0].location);">Get Link.</a>
</body>
</html>

点击“获取链接”后,会显示正确的链接。当用户在iframe内导航到另一个页面(在同一服务器上),再次单击“获取链接”时,它会显示当前的iframe网址。

但我需要获取iframe网址,而不必“点击”某些内容。

当我这样做时:

<html>
<body>
<iframe src="start.php"></iframe>
<script>alert(frames[0].location)</script>
</body>
</html>

警告信息显示“about:blank”

如何使用'frames [0] .location'传递到父页面可以读取的变量而不必使用onclick函数来获取iframe位置?

2 个答案:

答案 0 :(得分:0)

试试这个

alert(frames[0].contentWindow.location.href);

答案 1 :(得分:0)

您可以在需要的代码中直接使用frames[0].location。或者将其包装在函数内部。它没有直接在<script>标记中显示,因为当时尚未加载iframe。每当您在代码中查询frames[0].location时,都应该在该确切时间提供iframe的位置对象