JavaScript開發中的常用知識點

來源:互聯網
上載者:User

前言

最近在做項目的時候自己寫了一些JavaScipt代碼,於是自己又進行簡單的查閱資料整理了一下,發現了如下幾個比較有用的知識點:

1、三種聲明函數的方式

2、jQuery $(document).ready() 與window.onload的區別

3、location.href

4、擷取url中的參數

5、判斷是否存在函數的方法

6、設定Iframe高度

7、解決文字框設定唯讀後按退鍵後退頁面

1、三種聲明函數的方式

1、聲明式的函數

function functionName(arg1, arg2) {alert(arg1+arg2);}

2、匿名函數

var functionName1 = new Function("arg1", "arg2", "alert(arg1 + arg2)");

3、直接量函數

functionName2 = function (arg1, arg2) { alert(arg1 + arg2); };

三種函數調用方式

functionName("1", "22");

functionName1("2", "11");

functionName2("Hello", "World");

繼續看,還可以這樣寫,第一次體驗很不錯

functionName1.love = "Hello World!";

alert(functionName1.love);

functionName1.Test = function () { alert("Test"); }

functionName1.Test();

2、jQuery $(document).ready() 與window.onload的區別

1、執行時間

window.onload必須等到頁面內包括圖片的所有元素載入完畢後才能執行。

$(document).ready()是DOM結構繪製完畢後就執行,不必等到載入完畢。

2、編寫個數不同

window.onload不能同時編寫多個,如果有多個window.onload方法,只會執行一個,執行後面的一個。

$(document).ready()可以同時編寫多個,並且都可以得到執行。

3、簡化寫法

window.onload沒有簡化寫法 。

$(document).ready(function(){})可以簡寫成$(function(){})。

注意:window.onload和body.onload之間的關係和區別,最好自己親自去實踐一下。

3、location.href

1、top.location.href=”url”          在頂層頁面開啟url(跳出架構)

2、parent.location.href=”url”    在父視窗開啟Url地址

3、self.location.href=”url” 和 this.location.href=”url” 和 window.location.href=“url” 和location.href 都是在本頁面開啟url地址

由此我解決了一個問題:就是使用了Iframe架構,同時也控制使用者逾時重新登入的限制,所以有時候登入頁面會出現在頁面中的某部分。

處理方式就比較簡單就是在登入頁面通過JavaScript進行一次判斷即可

<script type="text/javascript">

if (window != top)

top.location.href = location.href;

</script>

查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/webkf/script/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.