首页 文章

在同一窗口和同一选项卡中打开URL

提问于
浏览
252

我想在同一窗口和包含带链接的页面的同一选项卡中打开一个链接 .

当我尝试使用 window.open 打开链接时,它会在新选项卡中打开 - 而不是在同一窗口的同一选项卡中 .

13 回答

  • 24

    为了确保在同一选项卡中打开链接,您应该使用 window.location.replace()

    请参阅以下示例:

    window.location.replace("http://www.w3schools.com");
    

    资料来源:http://www.w3schools.com/jsref/met_loc_replace.asp

  • 2

    您可以在不指定网址的情况下将其转到同一页面:

    window.open('?','_self');
    
  • 141

    如果你的页面在“frame”中,那么“Window.open('logout.aspx','_ self')”

    将被重定向到同一帧内 . 所以通过使用

    "Window.open('logout.aspx','_top')"
    

    我们可以将页面加载为新请求 .

  • 5

    您需要使用name属性:

    window.open("https://www.youraddress.com","_self")
    

    Edit :Url应该附加协议 . 没有它试图打开相对网址 . 在Chrome 59,Firefox 54和IE 11中测试过 .

  • 17

    用这个:

    location.href = "http://example.com";
    
  • 441

    其中一个最突出的JavaScript功能是即时启动onclick处理程序 . 我发现以下机制比使用 location.href=''location.reload()window.open 更可靠:

    // this function can fire onclick handler for any DOM-Element
    function fireClickEvent(element) {
        var evt = new window.MouseEvent('click', {
            view: window,
            bubbles: true,
            cancelable: true
        });
    
        element.dispatchEvent(evt);
    }
    
    // this function will setup a virtual anchor element
    // and fire click handler to open new URL in the same room
    // it works better than location.href=something or location.reload()
    function openNewURLInTheSameWindow(targetURL) {
        var a = document.createElement('a');
        a.href = targetURL;
        fireClickEvent(a);
    }
    

    上面的代码也有助于打开新的选项卡/窗口并绕过所有弹出窗口拦截器!例如 .

    function openNewTabOrNewWindow(targetURL) {
        var a = document.createElement('a');
        a.href = targetURL;
    
        a.target = '_blank'; // now it will open new tab/window and bypass any popup blocker!
    
        fireClickEvent(a);
    }
    
  • 1

    打开另一个网址,例如点击链接

    window.location.href = "http://example.com";
    
  • 1

    你必须使用 window.open 吗?怎么用 window.location="http://example.com"

  • 9

    window.open(url, wndname, params) ,它有三个参数 . 如果你不想在同一个窗口中打开它,只需设置一个不同的wndname . 如 :

    window.open(url1, "name1", params); // this open one window or tab
    window.open(url1, "name2", params); // though url is same, but it'll open in another window(tab).
    

    这是关于 window.open() 的详细信息,您可以信赖它!
    https://developer.mozilla.org/en/DOM/window.open

    试试~~

  • 9

    使用html 5,您可以使用history API .

    history.pushState({
      prevUrl: window.location.href
    
    }, 'Next page', 'http://localhost/x/next_page');
    history.go();
    

    然后在下一页上,您可以像这样访问状态对象

    let url = history.state.prevUrl;
    if (url) {
      console.log('user come from: '+ url)
    }
    
  • 21
    Just Try in button.
    
       <button onclick="location.reload();location.href='url_name'" 
       id="myButton" class="btn request-callback" >Explore More</button>
    
       Using href 
    
      <a href="#" class="know_how" onclick="location.reload();location.href='url_name'">Know More</a>
    
  • 2

    这很容易 . 打开第一个窗口 window.open(url, <tabNmae>)

    示例: window.open("abc.com",'myTab')

    并为下一个window.open,使用 same tab name 而不是 _self_parent 等 .

  • 7

    正是这样 window.open("www.youraddress.com","_self")

相关问题