假设我的 HTML 页面中有一个 iframe
。
<html>
<body>
This is my page
<button> Button </button>
<iframe src="www.example.com"></iframe>
</body>
</html>
因此用户可以在 iframe 中点击页面 www.example.com
上的链接。是否可以使用 JavaScript 来跟踪用户导航到哪些链接,然后当用户单击 Button
时,将这些链接发送到我的服务器?
最佳答案
您将无法从托管 iFrame 的页面跟踪 iFrame 中的事件。您可能会从 iFramed 页面内跟踪导航,然后使用 window.postMessage
将 URL 信息发送到父页面。这将需要您控制 iFrame 内的页面,并且您只能跟踪对您控制的页面的点击,并且这些页面已正确设置为向父级发送邮件。
您可以阅读有关[window.postMessage]
的更多信息following this link .需要注意的是,某些较旧的浏览器 (IE7) 不支持 postMessage,因此您可以使用类似 Window postMessage plugin 的库,它对这些浏览器使用 window.location.hash
轮询。我以前使用过这个库并取得了成功。
关于javascript - 在 iframe 中跟踪导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13421588/