1. 程式人生 > >js中top.location.href、parent.location.href用法

js中top.location.href、parent.location.href用法

在很多朋友不清楚在js中window.location.href、location.href、parent.location.href、top.location.href他們的用法,下面我來分別介紹。

window.location.href、location.href是本頁面跳轉

parent.location.href是上一層頁面跳轉

top.location.href是最外層的頁面跳轉

舉例說明:

window.location.href、location.href:

例:

 程式碼如下 複製程式碼

window.location.href= 'wapsend1.asp?zimu=A&rev= '   +   form1.rev.value ;

parent.location.href:C頁面跳轉

例:

 程式碼如下 複製程式碼

window.parent.parent.location.href=“你定義要跳轉的頁面”

top.location.href:A頁面跳轉

例:

在form提交表單的時候有個屬性可能對上面的(2)的情況有用target=_top

 程式碼如下 複製程式碼

<form name=loginForm action=Login.action method=post target=_top>

</form>

例2

 程式碼如下 複製程式碼

echo <iframe width=0 height=0 frameborder=0 scrolling=auto src='登入論壇' onload='reurl()'></iframe>;
echo <script> function reurl(){top.location.href='.$url.'}</script>;

也可以直接在表單提交是的target加個即可

<form>: form提交後D頁面跳轉

<form target=_blank>: form提交後彈出新頁面

<form target=_parent>: form提交後C頁面跳轉