使用JavaScript执行URL,但不要打开新窗口或选项卡

Use JavaScript to execute a URL, but do not open a new window or tab

本文关键字:新窗口 窗口 选项 URL 执行 JavaScript 使用      更新时间:2023-09-26

如何使用JS执行URL,但不打开新窗口或选项卡?

您可以使用以下JS在当前窗口中加载url。

window.location.assign("http://google.com")

有关更多信息,请查看https://developer.mozilla.org/en-US/docs/Web/API/window.location#Methods

如果您有一个js文件的URL,并且希望执行该内容,那么您可以直接将脚本元素附加到页面中。

var script = document.createElement("SCRIPT");
script.onload = function(){
    // do something on script load
};
document.head.appendChild(script);

这是我能理解的最好的问题。