js 學習筆記

來源:互聯網
上載者:User
 

 

document.lastModified   網頁最後修改的時間

document.write

輸出“的時候使用\轉義符號。

一些舊的瀏覽器不能瀏覽javascript不能演繹js或者js功能關閉了,或者js寫錯了,為了使js的文字在一些情況下不再網頁中出現,我們可以將js放在<!—及//à之內,例如:

<script language="JavaScript">

 

 

<!-- to hide the script that cannot work

document.write("Nice to meet you all.")

//-->

// This file was created on Jan 2004.

</script>

若功能失常,則作為html的解釋

window.open()

window.close()

window.moveTo()

window.moveBy()

window.resizeTo()

window.resizeBy()

 

window.open

子視窗的propery的可選值可設為yes(1)或no(0):

menubar=yes/no   1/0 是否要功能列,預設是no

toolbar                是否要工具列,躍攝時no

location                是否要位置輸入列,預設市no

directories=yes/no        是否預設連線按鍵列預設是no

status=yes/no            是否要狀態列,預設市no

scrollbars = yes/no        是否要捲軸

resizable                 是否能改變視窗大小

 

width

height

<html> <body bgcolor=bisque text=red>

 

<script>

adwin=window.open("","extrawin",

       "resizable=1,scrollbars=1,width=500,height=200")

adwin.document.open()

adwin.document.write("<html> <body bgcolor=beige text=blue>")

adwin.document.write("<center> <h1> Welcome to Takka Computer")

adwin.document.write("</h1> <p> <b> <font size=+2>")

adwin.document.write("Find your computer books here. </font> </b>")

adwin.document.write("</center> </body> </html>")

adwin.document.close()

document.write("Test: This should appear in the parent window. <p>")

</script>

<p> <h1> <center> This is parent window: winwrite.htm </center> </h1>

</body> </html>

 

在自視窗,父視窗的識別名稱是opener。即是開啟這個子視窗的來源視窗

<html> <body bgcolor=bisque text=red>

<script>

msgWin=window.open("","win2", "width=400,height=200,menubar=yes")

with(msgWin.document)

{open( )

write("<html> <body> <button ")

write("onClick=\'window.opener.document.bgColor=\"yellow\"\'>")

write("改變父視窗的背景顏色</button></body> </html>")

close( )

}

</script>

<p> <h1> <center> This is the parent window. </center> </h1>

</body> </html>

moveTo()是將視窗的左上方放在指定位置,

moveBy是在當前的位置再向下或向右移動若干個點

resizeTo() 重新設定視窗大小

resizeBy() 加大或減小

window.history.back() 返回

window.history.forward() 向前

window.stop()   stop

window.scrollTo(x,y) 頁面跳轉

window.scrollBy(x,y)

window.print() 列印

setTimeout()設定一個時間段

window.focus() 把滑鼠放到指定頁面

隱藏及不能關閉的網頁

  這類隱藏網頁一般是在一個主頁中用 window.open( ) 的方式來開啟, 目的是可以略去視窗的功能列, 開啟速度較快和較易收藏, 例如在 index.html 中有以下的程式碼:

window.open("hide.htm","win2",
"menubar=0,toolbar=0,location=0,directories=0,status=0,scrollbars=0,height=0,width=0");

  這是開啟一個新的視窗來載入 hide.htm, 而這新視窗的附件什麼也沒有, 高度和闊度都是 0 。

  在 hide.htm 可以有以下設定:

<html><head>

<script>
function hideIt()
{ window.resizeTo(0,0);
window.moveTo(2000,2000);
}
</script>
</head>

<body onLoad="hideIt( );" onreSize="hideIt( );" onMove="hideIt( );"
onUnload="window.open('hide.htm');">

</body></html>

  這處是在 <body> 的標籤中設定 onLoad (視窗開啟)、onreSize (視窗改變大小)、onMobve (視窗移動位置) 都會啟動 function hideIt( ), 在 function hideIt( ) 的執行事項是縮小視窗和移去 2000,2000 的位置。

 

  網頁隱藏後會做些什麼呢? 最常見的是定時 (例如每五分鐘) 彈出一個視窗來顯示廣告, 這過程維持至關機為止, 隨後有一章會說到 setTimeout( )setInterval( ) , 在隱藏視窗中使用這兩個 method, 就可定時執行一項功能。

 

  留意: 瀏覽器關閉時, 系統會記著視窗關閉時的大小和位置, 假若最後關閉的瀏覽器是 hide.htm, 以後的瀏覽器開啟時都會是縮小和移到 2000,2000 的位置, 若真的有這情況出現, 有什麼簡單的辦法可解決這問題 ?  顯示答案

 

用「記事本」設定一個 htm 檔案, 在內裡的 <script> 設定 window.resizeTo(600,600) window.moveTo(0,0) , 然後在「檔案總管」中開啟這檔案

 

status 是window的property 可以用window.status=”content”的方法在狀態列裡顯示內容

 

如果我們要瀏覽器執行我們的設定要加上return true

 

window的screen object

screen.height

screen.width

screen.availHeight 由上到下可給瀏覽器使用的像素

screen.availWidth 由左到右可給瀏覽器使用的像素

screen.colorDepth   可用的顏色數目

screen.pixelDepth   

 

location 是屬於window的object,是指網頁的來源。

location的兩個methods:reload()(重新載入);replace()(用指定網頁代替當前網頁)

“2”.sup();

escape( ) 是使用這語法: escape(string) , string 是要進行編碼的句子

 unescape( ) 的功能與 escape( ) 相反, 是將 escape 編碼變回字元, 並傳回這些字元, 例如以下的 script:

<html> <body>
<script>
str="
Number%3D%2323350%2C%20is%20that%20true%3F"
document.write( str + "<p>" )
    // ←沒有解碼的句子
document.write( unescape(str) )
  // ← 解碼的句子
</script> </body> </html>

eval( ) 是要瀏覽器進行工作, 使用的是這語法: eval(string) , string 是代表文字, 若這文字是 expression, eval( ) 會進行檢查, 若是 statement 就會執行這statement, 若是數目字, 會進行計算。

  請看這例子:

<html> <body>
<script>
x =
10
y =
20
result = eval ( x > y )
alert(result)
</script> </body> </html>

  這處的 eval(x > y ) 是要檢查 x 是否大過 y , 若是, 就傳回 true 這字, 若不是, 就傳回 false 這字, 所以在這例子, alert 對話盒中會出現 false 這字。 

  eval( ) 這功能最有用的地方是強迫瀏覽器將數字作為數值來處理, 而不是文字。例如我們設定一個文字框讓觀看者輸入數字, 瀏覽器可能會將數字作文字來處理, 若使用 eval( ) 就一定會作數字來處理。

isNAN( )

NaN 是 Not a Number 的簡寫, isNaN( ) 是用來檢查一項資料是否 "不是數字", 使用這語法: isNaN(testValue), 這 testValue 就是要檢查的資料, 若不是數字 (即是文字), 就傳回 true 這字, 若是數字, 就傳回 false.

Number( )

  這用來將一個物件變為數字, 使用這語法: Number(obj) , obj 是代表一個物件, 例如我們將一個 Date 物件放進 Number( ) 內, 如下:

<script>
x=new Date("
Dec 25, 1990")
x=Number(x)
alert(x)
</script>

parseInt( )

  這是 parse integer (抽取整數), 是將一個字串變為十進位的整數, 使用這語法: parseInt(string, radix), 這 radix 是這 string 的進位基數, 無指定就是十進位。若 parseInt( ) 遇到不是數值的字串, 就會傳回 NaN 的訊息。若遇到數字及字母混雜, 就會抽取至字母前的位置。

parseFloat( )

  這是 parse floating number (抽取浮點數), 是將一個字串之首的浮點數 (有小數點的數字) 抽出來, 使用這語法: parseInt(value), 若 parseFloat( ) 遇到不是數值的物件, 就會傳回 NaN 的訊息。

例 1 : 例如  x=parseFloat(13.12)   ( x 會等於 13.12 )

例 2 : 例如  x=parseFloat("13.12version")  ( x 會等於 13.12 )

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.