【问题标题】:How to capture all the user navigations from an external website opened in an IFrame? [duplicate]如何从 IFrame 中打开的外部网站捕获所有用户导航? [复制]
【发布时间】:2019-02-26 19:05:39
【问题描述】:

我能够将用户导航从在 IFrame 中打开的外部网站捕获到一个变量中,并且我能够在控制台中显示该位置。但我想将所有用户导航捕获到数组或文本文件中。请帮我解决一下这个。下面是我的代码。

<html>
<head>
<title>test page</title>
</head>

<body>

<li><a href="http://mysmallwebpage.com/" target="iframe_a">Small Page</li>

<iframe id = "frame" src="" name="iframe_a" onload ="loadImage()" width = 100% height = 100% style="border:none;"></iframe>

</body>

<script>

function loadImage() 
{

test = document.getElementById("frame").contentWindow.location; 

console.log(test);

}
</script>

</html>

脚本中的变量“TEST”持有导航链接。我只想捕获某个数组或文件中的所有导航。请使用控制台测试代码了解我的要求。

【问题讨论】:

    标签: javascript html iframe postmessage cross-domain-policy


    【解决方案1】:

    创建一个 empy 数组来保存导航历史记录。您可以使用push 方法向数组动态添加元素。

    在您的 loadImage 函数中,将当前位置推到数组的末尾。

    var navigations = [];
    
    function loadImage() 
    {
        navigations.push( document.getElementById("frame").contentWindow.location );
    
    }
    

    有关 JavaScript 数组的更多信息,请阅读 this article

    【讨论】:

    • 这是一个外部网站。这将引发跨域错误。
    • 它正在工作,但每次它都在更新数组的第 0 个索引。
    • 谁能帮我用 postmessage() 做这件事?听说它有助于解决跨域问题
    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    相关资源
    最近更新 更多