JavaScript 打开新链接的方式

不问归期 提交于 2019-12-06 03:08:19
<a onclick="window.open('http://www.runoob.com', '_blank', 'location=yes,height=570,width=520,scrollbars=yes,status=yes');">
1、点我在新窗口打开页面
</a>
<br>
<input type="button" value="加载新文档" onclick="newDoc()">
<input type="button" value="加载新頁面1" onclick="newhref()">
<input type="button" value="加载新頁面2" onclick="newopen()">
<input type="button" value="加载新頁面3" onclick="newopen2()">
    <input type="button" value="加载新頁面4" onclick="newopen3()">
    <input type="button" value="加载新頁面5" onclick="newopen4()">
    <input type="button" value="加载新頁面6" onclick="newreplace()">
<script>
function newDoc(){
    window.location.assign("http://www.w3cschool.cc");//在當前界面打開鏈接
}
    function newhref(){
    window.open("http://www.w3cschool.cc");//打開新窗口加載頁面
}
    function newopen(){
    window.location.href='http://www.runoob.com';//在當前界面打開鏈接
}
    function newopen2(){
    window.location='http://www.runoob.com';//在當前界面打開鏈接
}
    function newopen3(){
    location='http://www.runoob.com';//在當前界面打開鏈接
}
        function newopen4(){
    location.href='http://www.runoob.com';//在當前界面打開鏈接
}
    function newreplace(){
    window.location.replace("http://www.runoob.com");//在當前界面打開鏈接
}
</script>

assign()和replace()效果差不多,差別在於replace是置換掉現在的document,不會留下被置換掉的頁面紀錄,所以當你按下"上一頁"時回到的其實是你"上上個"看到的頁面....
這樣講一定聽不懂,舉個例來說好了:

當我從A頁面逛到B頁面後,在B頁面執行了location.replace(C頁面),會把當前的document從B換成C,所以當我點上一頁時就是回到A。示意:

A-->B
↓
location.replace(C)
↓
A-->C

而使用location.assign則會是這樣的情況:

A-->B
↓
location.assign(C)
↓
A-->B-->C

所以在C頁面點上一頁會回到B而不是A

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!