先贴代码
// window.location.href方式
window.location.href = 'https://home.oopsky.top';
// window.loction.replace方式
window.location.replace("https://home.oopsky.top");
// self.location方式
self.location.href = "https://home.oopsky.top";
self.location = "https://home.oopsky.top"; // herf 可以省略
// top.location方式
top.location.href = "https://home.oopsky.top";
top.location = "https://home.oopsky.top"; // herf 可以省略
// 延迟跳转页面(2s)
window.setTimeout("window.location='https://home.oopsky.top'", 2000);
延伸知识:JavaScript中的 window.parent ,window.top,window.self
在应用有frameset或者iframe的页面时,parent是父窗口,top是最顶级父窗口(有的窗口中套了好几层frameset或者iframe),self是当前窗口, opener只能是用open方法(window.open
)打开当前窗口的那个窗口。
window.self
功能:是对当前窗口自身的引用。它和
window
属性是等价的。语法:
window.self
注:
window
、self
、window.self
是等价的。window.top
功能:返回顶层窗口,即浏览器窗口。
语法:
window.top
注:如果窗口本身就是顶层窗口,
top
属性返回的是对自身的引用。window.parent
功能:返回父窗口。
语法:
window.parent
注:如果窗口本身是顶层窗口,
parent
属性返回的是对自身的引用。
在框架网页中,一般父窗口就是顶层窗口,但如果框架中还有框架,父窗口和顶层窗口就不一定相同了。
应当将框架视为窗口中的不同区域,框架是浏览器窗口中特定的部分。一个浏览器窗口可以根据你的需要分成任意多的框架,一个单个的框架也可以分成其它多个框架,即所谓的嵌套框架。
还有种操作,可以使用self == top
来判断自己的页面有没有被别人引用,
然后可以使用alert
提示用户或者用上面的方法强制跳转到你自己的页面喽。
举例:
下面代码保存为 test.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
<script>
if( self == top ) {
console.log("页面没有被嵌套呢")
} else {
top.location = self.location ;
}
</script>
</head>
<body>
</body>
</html>
如果test.html被加入iframe,则ifame所在页面将跳转到 test.html
location
:该对象包含当前url信息,拥有多个属性。默认属性为location.href
,即如果设置location="https://home.oopsky.top"
,则等同于location.href="https://home.oopsky.top"
。
参考文章: