【问题标题】:Open URL in same window and in same tab在同一窗口和同一选项卡中打开 URL
【发布时间】:2021-05-11 10:47:23
【问题描述】:

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

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

【问题讨论】:

  • 您可能想查看此post 以了解以下建议方法之间的差异,例如“相似”的_self_top

标签: javascript html hyperlink href


【解决方案1】:

你需要使用name属性:

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

编辑:Url 前应带有协议。没有它会尝试打开相对 url。在 Chrome 59、Firefox 54 和 IE 11 中测试。

【讨论】:

  • 第二个参数只是新窗口的名称,就像标签a的属性target=一样。事实上,您可以随意命名您的窗口。您所需要的只是将其设置为不同的值,这样它就不会在同一个窗口或选项卡中打开。
  • @ijse 实际上,有一些特殊的名称,其中之一是'_self',它指的是代码运行所在的win/tab。;)
  • '_self' 未在 MDN [developer.mozilla.org/en-US/docs/Web/API/Window/open] window.open() 上的文档中指定。一个更跨浏览器的解决方案是使用 location.replace()。
  • 上述评论中的 MDN 链接自动链接到 404。链接为 developer.mozilla.org/en-US/docs/Web/API/Window/open
  • _selfHTML5 W3C 建议 2014 年 10 月 28 日5.1.6 浏览上下文名称 部分中提到:w3.org/TR/html/browsers.html#browsing-context-names(但window.location 仍然更干净)。
【解决方案2】:

使用这个:

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

【讨论】:

【解决方案3】:

为了确保链接在同一个标​​签页中打开,你应该使用window.location.replace()

请看下面的例子:

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

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

【讨论】:

  • 它不保存浏览历史,我们不应该使用它。而是尝试 window.open("google.com","_top") 参考链接geeksforgeeks.org/…
  • 我将它用作内部页面的内部插件的一部分,它非常完美。浏览器历史记录并不是真正的问题,因为它用于自动化,使用它的人无需手动访问任何页面。谢谢sn-p!
【解决方案4】:

你可以在不指定 url 的情况下让它进入同一个页面:

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

【讨论】:

  • 这不是同一页。它将从现有 URL 中删除任何查询字符串。
【解决方案5】:

如果您的页面位于“框架”内,那么 "Window.open('logout.aspx','_self')"

将在同一帧内重定向。所以通过使用

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

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

【讨论】:

    【解决方案6】:

    最突出的 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);
    }
    

    【讨论】:

      【解决方案7】:

      像点击链接一样打开另一个网址

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

      【讨论】:

        【解决方案8】:

        你必须使用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

          试试看~~

          【讨论】:

          • 这个问题很清楚,想在同一个窗口同一个标签
          【解决方案10】:

          使用 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)
          }
          

          【讨论】:

            【解决方案11】:

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

            【讨论】:

              【解决方案12】:

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

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

              对于接下来的所有 window.open,使用 相同的选项卡名称 而不是 _self_parent 等。

              【讨论】:

                【解决方案13】:
                   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> 
                

                【讨论】:

                • 触发当前页面的重新加载然后在下一条语句中通过加载新页面来取消它是完全没有意义的。
                【解决方案14】:

                正如 MDN 的 refs 所说,只需给新的 window/tab 命名。

                https://developer.mozilla.org/en-US/docs/Web/API/Window/open#Syntax

                使用_self在当前标签页中打开

                <a
                 href="url"
                 target="_self">
                   open
                </a>
                
                const autoOpenAlink = (url = ``) => {
                  window.open(url, "open testing page in the same tab page");
                }
                
                

                使用_blank在新标签页中打开

                vue 演示

                    <div style="margin: 5px;">
                        <a
                            :href="url"
                            @click="autoOpenAlink"
                            target="_blank"
                            >
                            {{url}}
                        </a>
                    </div>
                

                vue

                    autoOpenAlink(e) {
                        e.preventDefault();
                        let url = this.url;
                        window.open(url, "iframe testing page");
                    },
                

                【讨论】:

                • 您无法可靠地知道当前窗口的名称。最好听从this 2011 answer 的建议并使用特殊的_self 名称。
                • 你的第一个例子是错误的。 _blank 明确地是一个未命名的 new 窗口或选项卡。
                • 一点也不!如果我设置_self,它将在当前页面打开,这对我不利,我只需要一个新页面。
                • 问题不在于需要什么!如果您想回答有关您需要什么的问题,请找到一个相关的问题。
                【解决方案15】:

                你可以的

                window.close();
                window.open("index.html");
                

                它在我的网站上成功运行。

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-02-18
                  • 2016-01-27
                  • 2010-09-12
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多