Location.assign()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Location.assign() 方法会触发窗口加载并显示指定的 URL 的内容。

如果由于安全原因无法执行跳转,那么会抛出一个 SECURITY_ERROR 类型的 DOMException。当调用此方法的脚本来源和页面的 Location 对象中定义的来源隶属于不同域的时候,就会抛出上述错误。

如果传入了一个无效的 URL,则会抛出一个 SYNTAX_ERROR 类型的 DOMException

语法

location.assign(url);

参数

url

一个包含了要跳转到的链接的DOMString

示例

js
// 跳转到 Location.reload() 这篇文章
document.location.assign(
  "https://developer.mozilla.org/zh-CN/docs/Web/API/Location/reload",
);

规范

Specification
HTML
# dom-location-assign-dev

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
assign

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
No support
No support
User must explicitly enable this feature.

相关内容