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

JavaScript HTML

Tom凯

2020-03-23

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

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

第3078篇《在同一窗口和同一选项卡中打开URL》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

7个回答
小宇宙Gil 2020.03.23

完全像这样 window.open("www.youraddress.com","_self")

Gil 2020.03.23

正如MDN的裁判所说,只需给出新的window/ 的名称即可tab

https://developer.mozilla.org/zh-CN/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");
}

open in a new tab page using _blank

vue demo

    <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");
    },
神奇小宇宙 2020.03.23

你要用window.open吗?那使用window.location="http://example.com"呢?

猴子村村 2020.03.23

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

window.location.href = "http://example.com";
泡芙西门 2020.03.23

用这个:

location.href = "http://example.com";
斯丁前端 2020.03.23

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

请参见下面的示例:

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

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

猪猪 2020.03.23

您需要使用name属性:

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

编辑:URL应该在协议之前。没有它,则尝试打开相对URL。经过Chrome 59,Firefox 54和IE 11的测试。

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android