【问题标题】:JavaScript how to open new window then change its URL? [duplicate]JavaScript如何打开新窗口然后更改其URL? [复制]
【发布时间】:2013-06-04 14:14:55
【问题描述】:
<script language=JavaScript>
winRef = new Object();
winRef.closed = true;
</script>

<form name=f1 id=f1>
<input type=button name=b1 id=b1 value="New window" 
onClick="winRef=window.open('http://www.google.com/','myNewWindow',
  'left=15,top=15,width=510,height=420,toolbar=1,menubar=1,resizable=1,status=1');
  winRef.focus()">

<input type=button name=b2 id=b2 value="Close the window" 
onClick="if(!winRef.closed)winRef.close();">

<input type=button name=b3 id=b3 value="Check: is it closed?" 
onClick="alert(winRef.closed ? 'It\'s CLOSED!':'It\'s still OPEN!');">

<input type=button name=b4 id=b4 value="Change new window URL" 
onClick="winRef.location.href('http://example.com/' + winRef.location.href)">
</form>

此代码在新窗口中打开谷歌然后检查它是否打开并关闭它。我怎么能把它的名字改成“http://example.com/?url=http://www.google.com/” 我需要将我的网站网址放在打开的窗口网址前面

【问题讨论】:

  • href 不是函数

标签: javascript url location


【解决方案1】:

代码如下:

    <script language="javascript">
var strWindowFeatures = "menubar=no,location=no,resizable=no,scrollbars=no,status=no,width=200,height=200";
var nome='nameofme';
 function vamos(url1){
    win = window.open(url1,nome,strWindowFeatures);
    win.focus();
    // If the window opened successfully (e.g: not blocked)
    if (win) {
        win.onload = function() {//onload can be blocked by the brower
            win.location.href='http://www.bing.com';
        };
    }
    setTimeout("win.location.href='http://www.bing.com'",4000);//change the href after 4 second
    win.focus();
    window.history.replaceState('Object', 'Title', '?http://www.bing.com');
    setTimeout("window.history.replaceState('Object', 'Title', '?http://www.fava.com')",4000);//after 4 sec
}
</script>
<button id="dd" onClick="vamos('http://www.google.com')">but</button>

这里是jsfiddle,请记住,在小提琴中,window.history.replaceState 不起作用

【讨论】:

  • 很好,但我真正需要的是重定向后的链接,例如:打开后 www.google.com 将被重定向到 www.google.com.uk 所以我需要页面后的最后一个链接链接 www.example.com/?www.google.com.uk
  • @user2443940 您需要在一段时间后或仅在新窗口打开时重定向用户?
  • 但是如何将我的链接与当前链接放在一起?
  • @user2443940 代码更新,请检查
  • 当我使用 window.open() 打开一个新窗口时,我希望允许用户在他们想要导航时更改 URL。我该如何做到这一点?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多