天天看點

JavaScript 跳轉頁面與打開新視窗的方法

  1. 超連結

    <a href="http://www.jb51.net" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" title="腳本之家">Welcome</a>

    等效于js代碼

    window.location.href=“http://www.jb51.net”; //在同目前視窗中打開視窗

  2. 超連結

    <a href="http://www.jb51.net" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" title="腳本之家" target="_blank">Welcome</a>

    等效于js代碼

    window.open(“http://www.jb51.net”); //在另外建立視窗中打開視窗

    //詳細介紹

    第一種:

<script language="JavaScript" type="text/javascript">
           window.location.href="http://www.dollare.com.cn/login.PHP?backurl=" target="_blank" rel="external nofollow" +window.location.href; 
    </script>
           

第二種:

<script language="javascript">
alert("傳回");
window.history.back(-1);
   </script>
           

第三種:

<script language="javascript">
window.navigate("dollare.php");
  </script>
           

第四種:

<script language="JavaScript">
          self.location='dollare.htm';
   </script>
           

第五種:

<script language="javascript">
          alert("非法通路!");
          top.location='dollare.html;
   </script>
           

1.在原來的窗體中直接跳轉用

window.location.href=“你所要跳轉的頁面”;

2、在新窗體中打開頁面用:

window.open(‘你所要跳轉的頁面’);

window.history.back(-1);傳回上一頁

3、一些用法

按鈕式:

<INPUT name="pclog" type="button" value="/Go" onClick="location.href='連接配接位址'">
           

連結式:

<a href="javascript:history.go(-1)" target="_blank" rel="external nofollow" >傳回上一頁</a>

<a href="<%=Request.ServerVariables(" target="_blank" rel="external nofollow" HTTP_REFERER")%>">傳回上一頁</a>
           

直接跳轉式:

<script>window.location.href='連接配接位址';</script>

開新視窗:

<a href="/javascript:" target="_blank" rel="external nofollow"  onClick="window.open('http://www.dolalre.com.cn','','height=500,width=611,scrollbars=yes,status =yes')">123</a>
           
<SCRIPT>  
<!--  
window.open  

('dollare.html','newwindow','height=100,width=400,top=0,left=0,toolbar=no,menubar=no,scrollbars=no,  

resizable=no,location=no, status=no')  
//寫成一行  
-->  
</SCRIPT> 
           

腳本運作後,page.html将在新窗體newwindow中打開,寬為100,高為400,距屏頂0象素,屏左0象素,無工具條,無菜單條,無滾動條,不可調整大小,無位址欄,無狀态欄。請對照。上例中涉及的為常用的幾個參數,除此以外還有很多其他參數,請見四。

四、各項參數

  其中yes/no也可使用1/0;pixel value為具體的數值,機關象素。

參數 取值範圍 說明
alwaysLowered yes/no 指定視窗隐藏在所有視窗之後
alwaysRaised yes/no 指定視窗懸浮在所有視窗之上
depended yes/no 是否和父視窗同時關閉
directories yes/no Nav2和3的目錄欄是否可見
height pixel value 視窗高度
hotkeys yes/no 在沒菜單欄的視窗中設安全退出熱鍵
innerHeight pixel value 視窗中文檔的像素高度
innerWidth pixel value 視窗中文檔的像素寬度
location yes/no 位置欄是否可見
menubar yes/no 菜單欄是否可見
outerHeight pixel value 設定視窗(包括裝飾邊框)的像素高度
outerWidth pixel value 設定視窗(包括裝飾邊框)的像素寬度
resizable yes/no 視窗大小是否可調整
screenX pixel value 視窗距螢幕左邊界的像素長度
screenY pixel value 視窗距螢幕上邊界的像素長度
scrollbars yes/no 視窗是否可有卷軸框
titlebar yes/no 視窗題目欄是否可見
toolbar yes/no 視窗工具欄是否可見
Width pixel value 視窗的像素寬度
z-look yes/no 視窗被激活後是否浮在其它視窗之上

1、最基本的彈出視窗代碼】

  其實代碼非常簡單:

<SCRIPT LANGUAGE="javascript">  
<!--  
window.open ('dollare.html')  
-->  
</SCRIPT> 
           

因為着是一段javascripts代碼,是以它們應該放在<SCRIPT LANGUAGE=“javascript”>标簽和</script>之間 。<!-- 和 -->是對一些版本低的浏覽器起作用,在這些老浏覽器中不會将标簽中的代碼作為文本顯示出來。要養成這個好習慣啊。 Window.open (‘dollare.html’) 用于控制彈出新的視窗page.html,如果page.html不與主視窗在同一路徑下, 前面應寫明路徑,絕對路徑(http://www.dollare.com.cn/dollare.html)和相對路徑(…/)均可。用單引号和雙引号都可以,隻是不要混用。 這一段代碼可以加入HTML的任意位置,<head>和</head>之間可以,<body>間</body$gt;也可以,越前越早執行,尤其是頁面代碼長,又想使頁面早點彈出就盡量往前放。

2、經過設定後的彈出視窗

下面再說一說彈出視窗的設定。隻要再往上面的代碼中加一點東西就可以了。

我們來定制這個彈出的視窗的外觀,尺寸大小,彈出的位置以适應該頁面的具體情況。

<SCRIPT LANGUAGE="javascript">  
<!--  
window.open ('dollare.html', 'newwindow', 'height=100, width=400, top=0,left=0, toolbar=no,  

menubar=no, scrollbars=no, resizable=no,location=no, status=no')  
//寫成一行  
-->  
</SCRIPT> 
           
參數解釋:  
<SCRIPT LANGUAGE="javascript"> js腳本開始;  
window.open 彈出新視窗的指令;  
'page.html' 彈出視窗的檔案名;  
'newwindow' 彈出視窗的名字(不是檔案名),非必須,可用空'代替;  
height=100 視窗高度;  
width=400 視窗寬度;  
top=0 視窗距離螢幕上方的象素值;  
left=0 視窗距離螢幕左側的象素值;  

width=250,toolbar=no,scrollbars="+scroll+",menubar=no");  
//寫成一行  
OpenWindow.document.write("<TITLE>dollare雄仔工作室</TITLE>")  
OpenWindow.document.write("<BODY BGCOLOR=#ffffff>")  
OpenWindow.document.write("<h1>Hello!</h1>")  
OpenWindow.document.write("New window opened!<br> is Good")  
OpenWindow.document.write("</BODY>")  
OpenWindow.document.write("</HTML>")  
OpenWindow.document.close()}  
</SCRIPT>  
</head>  
<body>  
<a href="#" target="_blank" rel="external nofollow"  onclick="openwin()">打開一個視窗</a>  
<input type="button" onclick="openwin()" value="打開視窗">  
</body>  
</html> 
           

看看 OpenWindow.document.write()裡面的代碼不就是标準的HTML嗎?隻要按照格式寫更多的行即可。千萬注意多一個标簽或少一個标簽就會出現錯誤。記得用OpenWindow.document.close()結束啊。

9、終極應用–彈出的視窗之Cookie控制

  回想一下,上面的彈出視窗雖然酷,但是有一點小毛病(沉浸在喜悅之中,一定沒有發現吧?)比如你将上面的腳本放在一個需要頻繁經過的頁面裡(例如首頁),那麼每次重新整理這個頁面,視窗都會彈出一次,是不是非常煩人?:-(有解決的辦法嗎?Yes! 😉 Follow me. 我們使用cookie來控制一下就可以了。首先,将如下代碼加入首頁面HTML的<HEAD>區:

<script>  
function openwin()  
{window.open("page.html","","width=200,height=200")}  
function get_cookie(Name)  
{var search = Name + "="  
var returnvalue = "";  
if (document.cookie.length > 0) {  
offset = document.cookie.indexOf(search)  
if (offset != -1) {  
offset += search.length  
end = document.cookie.indexOf(";", offset);  
if (end == -1)  
end = document.cookie.length;  
returnvalue=unescape(document.cookie.substring(offset,end))  
}  
}  
return returnvalue;  
}  
function loadpopup(){  
if (get_cookie('popped')=='){  
openwin()  
document.cookie="popped=yes"  
}  
}  
</script> 
           

然後,用<body οnlοad=“loadpopup()”>(注意不是openwin而是loadpop啊!)替換首頁面中原有的<BODY>這一句即可。你可以試着重新整理一下這個頁面或重新進入該頁面,視窗再也不會彈出了。真正的Pop-Only-Once!

繼續閱讀