DIV CSS 佈局教程網

JavaScript 基礎問答(四):導航功能實作大全
編輯:JavaScript基礎知識     

一、下拉選單式連結

Q:如何讓下拉選單在切換選項時直接跳到對應頁面?

A:關鍵是監聽 select 的 change 事件,把選中選項的 value 指定給 location。完整寫法:

<form>
<select onChange="if(this.selectedIndex!=0)
  self.location=this.options[this.selectedIndex].value">
  <option value="" selected>選擇一個頁面
  <option value="startpag.htm">JavaScript FAQ
  <option value="numbers.htm">Numbers
  <option value="strings.htm">Strings
  <option value="navigati.htm">Navigation
  <option value="colors.htm">Colors
  <option value="https://www.javascripter.net">JavaScripter.net
</select>
</form>

把選項與網址換成你需要的即可,絕對路徑(如 https://www.javascripter.net)與相對路徑(如 mypage.htm)都支援。那個 selectedIndex != 0 的判斷,是為了避開使用者選回「請選擇」那一項時誤跳。

二、按鈕連結

Q:怎麼把按鈕變成指向別頁的超連結?

A:用 type=button 搭配 onclick 改寫 location:

<form>
<input type=button value="點我前往"
  onClick="self.location='Your_URL_here.htm'">
</form>

換掉按鈕文字與目標網址就能用,相對路徑如 mypage.htm 也沒問題。

三、後退按鈕

Q:能讓按鈕像瀏覽器的「後退」一樣嗎?

A:呼叫 history 物件即可:

<form>
<input type=button value="Back" onCLick="history.back()">
</form>

四、前進按鈕

Q:那「前進」呢?

A:同理:

<form>
<input type=button value="Forward" onCLick="history.forward()">
</form>

要留意一個前提:如果瀏覽器本身的「前進」鈕是灰色的,這個自製按鈕也不會有反應。這發生在你目前這一頁就是整個歷史紀錄的最後一筆時。換句話說,只有當你是用「後退」回到目前頁面的(不論是瀏覽器的後退鈕還是你自製的),前進才有意義。

五、讀取查詢字串

Q:腳本能取得目前 URL 中的查詢字串嗎?

A:可以,用 location.search。查詢字串是 URL 中接在檔名之後、以問號引導的選用部分,例如:

https://www.myfirm.com/file.html?myquery

讀取方式:

alert('Query string: ' + self.location.search);

若需要動態組出帶查詢字串的網址,可以拆成 protocol、host、pathname 三段再拼回去:

self.location = self.location.protocol + '//' +
  self.location.host + self.location.pathname + '?test';

注意:查詢字串並非在所有環境都如預期運作。例如把頁面存到本機硬碟後,這類寫法在 Internet Explorer 4.x 上會失效(Netscape Navigator 則仍然正常)。

六、頁面之間傳遞參數

Q:可以從一個頁面把參數傳到另一個頁面嗎?

A:有幾種常見路徑:

  • 寫入 Cookie
  • 存在另一個視窗或框架的變數中
  • 塞進可寫的 top.name(視窗名稱)屬性
  • 以查詢字串接在目標頁網址後面

以下範例一次示範前三種,並用第四種導向目標頁:

<input type=button value="前往並讀取參數"
  onclick="SetCookie('parm_value','It_worked',1);
           top.parm_value='It_worked';
           top.name='It_worked';
           self.location='parm_get.htm?'+escape('It_worked')">

目標頁再以 escape() 的反向操作還原值即可。

七、搜尋頁面文字

Q:如何在頁面中搜尋特定的字串?

A:早期兩大瀏覽器做法不同:Netscape Navigator 4.x 用 window.find(string);Internet Explorer 4.x 以上則建立文字範圍物件,再呼叫 TRang.findText(string)。

以下範例依使用者輸入搜尋並高亮:

<form name="f1" action=""
  onSubmit="if(this.t1.value!=null && this.t1.value!='')
    findString(this.t1.value);return false">
  <input type="text" name=t1 value="" size=20>
  <input type="submit" name=b1 value="Find">
</form>

<script language="JavaScript">
<!--
var TRange=null

function findString (str) {
 if (parseInt(navigator.appVersion)<4) return;
 var strFound;
 if (navigator.appName=="Netscape") {
   // NAVIGATOR 專用
   strFound=self.find(str);
   if (!strFound) {
     strFound=self.find(str,0,1)
     while (self.find(str,0,1)) continue
   }
 }
 if (navigator.appName.indexOf("Microsoft")!=-1) {
   // EXPLORER 專用
   if (TRange!=null) {
     TRange.collapse(false)
     strFound=TRange.findText(str)
     if (strFound) TRange.select()
   }
   if (TRange==null || strFound==0) {
     TRange=self.document.body.createTextRange()
     strFound=TRange.findText(str)
     if (strFound) TRange.select()
   }
 }
 if (!strFound) alert ("String '"+str+"' not found!")
}
//-->
</script>

這段程式是個很好的歷史切片:它清楚展示了在標準尚未統一之前,同一個功能往往得寫兩套分支。今天則有 window.find() 的標準版本與 Selection API 可用,不必再這樣分頭處理。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved