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

无法使用javascript找到新窗口的href,javascript,Javascript,我使用下面提到的脚本获取新打开窗口的href,但它不工作 浏览器FF 7,即9,赢7 有人能告诉我剧本里出了什么问题吗 提前谢谢 阿克希尔 函数open_win() { var mywindow=window.open(“http://thesun.co.uk"); var clbutton=document.createElement(“按钮”); var par=document.getElementById(“主”); clbutton.setAttribute(“id”、“焦点”);

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

浏览器FF 7,即9,赢7

有人能告诉我剧本里出了什么问题吗

提前谢谢

阿克希尔


函数open_win()
{
var mywindow=window.open(“http://thesun.co.uk");
var clbutton=document.createElement(“按钮”);
var par=document.getElementById(“主”);
clbutton.setAttribute(“id”、“焦点”);
clbutton.setAttribute(“值”、“单击”);
clbutton.innerHTML=“单击”;
前插入(CLUBN,NULL);
clbutton.onclick=function(){alertMsg(mywindow);}
}
函数alertMsg(mywindow)
{
mywindow.focus();
var t=mywindow.location.href;
警报(t);
//mywindow.alert(“测试”);
mywindow.close();
}


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

您无法获取与正在运行的脚本不同的域或协议中的站点的URL。这是由于跨域限制。Chrome会给您以下错误:
不安全的JavaScript试图从带有URL的帧访问带有URL的帧。域、协议和端口必须匹配。

您的控制台中是否出现“不安全的JavaScript尝试从带有URL的帧访问带有URL的帧…”或“mywindow为null”消息?是否有任何解决方案,我刚刚在FF的错误控制台中检查了它的错误:“访问属性的权限被拒绝”href“”,此外,如果我正在使用myWindow.onload='some alert function',那么在实际加载sun.co.uk页面之前也会发出警报,似乎它无法识别新窗口,就像at MDC在使用
窗口打开的窗口上声明您的操作一样。open
受。没有办法绕过同源策略,因为它是所有浏览器的重要安全特性。
    <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>