如何在子窗口中检测location.hostname?

时间:2018-07-29 19:48:39

标签: javascript jquery

我需要在javascript(或JQuery)中从子窗口标签中检测实际的网址。

示例:

parent.php

<script type="text/javascript">

    function openInNewTab(url) {
        var child = window.open(url, '_blank');
        child.focus();
        var timer = setInterval(checkChild, 500);

        function checkChild() {

            document.write("Show here the location.hostname from child window");

        }

    }

</script>

<button onclick="openInNewTab('//mypage.com/child.php');">Open Child</button>

child.php

<a href="//google.com">Go to google</a>
<a href="//facebook.com">Go to facebook</a>

如果用户打开child.php,则在parent.php“ mypage.com”中显示,但如果用户按下,则在parent.php“ google”中单击“ 转到Google ” .com ”或在“ 转到Facebook ”中显示“ facebook.com

我需要使用javascript(或JQuery)

1 个答案:

答案 0 :(得分:0)

尝试

function openInNewTab(url) {
    var child = window.open(url, '_blank');
    child.focus();
    var timer = setInterval(function(){
        checkChild(child);      
    }, 500);

    function checkChild(child) {
        document.write(child.location.href , "   ");
    }

}