我有这段代码,可以成功跟踪 url 在 iframe 中的更改:
<html>
<body>
<iframe src="start.php"></iframe>
<a href="#" onclick="alert(frames[0].location);">Get Link.</a>
</body>
</html>
当点击“获取链接”时,它会显示正确的链接。当用户在 iframe 内导航到另一个页面(在同一服务器上)并再次单击“获取链接”时,它会显示当前的 iframe url。
但我需要获取 iframe URL,而不必“单击”某些内容。
当我这样做时:
<html>
<body>
<iframe src="start.php"></iframe>
<script>alert(frames[0].location)</script>
</body>
</html>
警报消息显示“about:blank”
如何使用传递到父页面可以读取的变量的“frames[0].location”来获取 iframe 位置,而无需使用 onclick 函数?
最佳答案
试试这个
alert(frames[0].contentWindow.location.href);
关于javascript - onclick v 没有 onclick;如何在不点击的情况下获得结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25694094/