无法使用javascript找到新窗口的href

Unable to find href of new window using javascript

本文关键字:窗口 href 新窗口 javascript      更新时间:2023-09-26

我正在使用下面提到的脚本来获取新打开的窗口的href,但它不起作用

浏览器ff7, ie9, win7

谁能让我知道什么是错误的脚本

Thanks in Advance

(Akhil

    <html>
    <head>
    <script type="text/javascript">
    function open_win() 
    {
      var mywindow=window.open("http://thesun.co.uk");
      var clbutton=document.createElement("button");
      var par=document.getElementById("main");
      clbutton.setAttribute("id","focus");
      clbutton.setAttribute("value","CLICK");
      clbutton.innerHTML="CLICK";
      par.insertBefore(clbutton,null);
      clbutton.onclick=function() {alertMsg(mywindow);}
    }
    function alertMsg(mywindow)
    {
  mywindow.focus();
  var t=mywindow.location.href;
  alert(t);
  //mywindow.alert("testing");
  mywindow.close();
    }
    </script>
    </head>
      <body onload="open_win()">
         <p id="main"></p>
      </body>
    </html>

您无法获得与您正在运行的脚本在不同域或协议中的站点的URL。这是由于跨域限制。Chrome给你以下错误:不安全的JavaScript试图从URL为http://fiddle.jshell.net/_display/的框架访问URL为http://www.thesun.co.uk/sol/homepage/的框架。域、协议和端口必须匹配

我认为这可能是因为变量尚未设置。我认为你可能需要一个回调来说明dom已经为新窗口准备好了