天天看點

JavaScript基礎知識(四)

31 設定日期輸出格式

1: <script language=”javascript”>

2: var thisdate = new date();

3: var thistimestring = thisdate.gethours() + “:” + thisdate.getminutes();

4: var thisdatestring = thisdate.getfullyear() + “/” + thisdate.getmonth() + “/” + thisdate.getdate();

5: document.write(thistimestring + “ on “ + thisdatestring);

6: </script>

32 讀取url參數

2: var urlparts = document.url.split(“?”);

3: var parameterparts = urlparts[1].split(“&”);

4: for (i = 0; i < parameterparts.length; i++) {

5: var pairparts = parameterparts.split(“=”);

6: var pairname = pairparts[0];

7: var pairvalue = pairparts[1];

8: document.write(pairname + “ :“ +pairvalue );

9: }

10: </script>

你還以為html是無狀态的麼?

33 打開一個新的document對象

2: function newdocument() {

3: document.open();

4: document.write(“<p>this is a new document.</p>”);

5: document.close();

6: }

7: </script>

34 頁面跳轉

2: window.location = “[url]http://www.liu21st.com/[/url]”;

3: </script>

35 添加網頁加載進度視窗

1: <html>

2: <head>

3: <script language='javascript'>

4: var placeholder = window.open('holder.html','placeholder','width=200,height=200');

5: </script>

6: <title>the main page</title>

7: </head>

8: <body>

9: <p>this is the main page</p>

10: </body>

11: </html>

javascript就這麼回事3:圖像

36 讀取圖像屬性

1: <img src="/”p_w_picpath1.jpg"” name=”myimage”>

2: <a href=”# ” onclick=”window.alert(document.myimage.width)”>width</a>

3:

37 動态加載圖像

2: myimage = new image;

3: myimage.src = “tellers1.jpg”;

4: </script>

38 簡單的圖像替換

2: rollimage = new image;

3: rollimage.src = “rollimage1.jpg”;

4: defaultimage = new image;

5: defaultimage.src = “p_w_picpath1.jpg”;

7: <a href="/”myurl"” onmouseover=”document.myimage.src = rollimage.src;”

8: onmouseout=”document.myimage.src = defaultimage.src;”>

9: <img src="/”p_w_picpath1.jpg"” name=”myimage” width=100 height=100 border=0>

39 随機顯示圖像

2: var p_w_picpathlist = new array;

3: p_w_picpathlist[0] = “p_w_picpath1.jpg”;

4: p_w_picpathlist[1] = “p_w_picpath2.jpg”;

5: p_w_picpathlist[2] = “p_w_picpath3.jpg”;

6: p_w_picpathlist[3] = “p_w_picpath4.jpg”;

7: var p_w_picpathchoice = math.floor(math.random() * p_w_picpathlist.length);

8: document.write(‘<img src=”’ + p_w_picpathlist[p_w_picpathchoice] + ‘“>’);

9: </script>

40 函數實作的圖像替換

2: var source = 0;

3: var replacement = 1;

4: function createrollover(originalimage,replacementimage) {

5: var p_w_picpatharray = new array;

6: p_w_picpatharray[source] = new image;

7: p_w_picpatharray[source].src = originalimage;

8: p_w_picpatharray[replacement] = new image;

9: p_w_picpatharray[replacement].src = replacementimage;

10: return p_w_picpatharray;

11: }

12: var rollimage1 = createrollover(“p_w_picpath1.jpg”,”rollimage1.jpg”);

13: </script>

14: <a href=”#” onmouseover=”document.myimage1.src = rollimage1[replacement].src;”

15: onmouseout=”document.myimage1.src = rollimage1[source].src;”>

16: <img src="/”p_w_picpath1.jpg"” width=100 name=”myimage1” border=0>

17: </a>

繼續閱讀