1,動態執行js
? (1)document.write("<script>alert('ok');</script"+">");注意</script>要分開寫
? 或
? (2)var oDiv=document.createElement("script");
?????????? oDiv.appendChild("alert('0k')");
??????? document.body.appendChild(oDiv);
? (3)eval("alert(ok);");
???? 但不知何故,在模態窗口中用eval執行window.dialogArguments.location.reload()會報錯
?詳見
http://hi.baidu.com/ziyou038/blog/item/38b25b540a777e57564e009d.html? (4)不要再回調中執行document.write
?原因:document close后,再document.write,就會覆蓋整個頁面
2//在寫JavaScript代碼時,可加入一個文本區域來進行調試
??? function debugInfo(info) {
????? var debugWindow = document.getElementById("debug_window");
????? debugWindow.value = debugWindow.value + "\r\n" + info;
?? }
3,
有時候,我們在調用 .js文件的時候,會發現。.js文件里的中文變成亂碼了,
其實只要在調用的時候加一個charset就行了
<script language="javascript" src="../jscript/Cjs.js" type="text/javascript"
charset="gb2312"></script>
4,
function?b()
{
????i?=?3;
}
function?a()
{
????i?=?1;
????b();
????alert("i="+i);
}
a();
a()和b()里面的變量i都沒有使用var聲明,最終alert結果會是3!!!
和 shell 一樣的,局部中聲明了變量其實是全局的, 加了 var 才是私有的
來源:
http://www.tkk7.com/vls/archive/2008/05/11/199808.html5,table在后面添加加行或列,通用寫法insertRow(-1),insertCell(-1),這樣才能跨瀏覽器
6,native2ascii
??? 先用alert(escape("確定")) 的方式得到"%u786E%u5B9A",然后在
js中alert(unescape('%u786E%u5B9A'))即可。
??? 如果是從java中得到的"\
u786E\u5B9A"
<script>
var str="\u786E\u5B9A";
str=str.replace(/\\/,"%");
var a=unescape(str)
document.write(a);
</script>
7,在JavaScript中不要使用跟HTML的id一樣的變量名。否則IE會報I對象未定義的錯誤
8,var?fileName?=?"This?is?a?title".replace(/?/g,"_");??
9,parseInt("09")有問題,應該為parseInt("09",?10)
http://www.javaeye.com/topic/200401
10,執行focus()的時候,元素尚未可用。因此要延遲執行:
var?newInput?=?document.createElement("input");??
newInput.id?=?"TheNewInput";??
document.body.appendChild(newInput);??
//在0.01秒之后調用匿名函數獲取焦點??
setTimeout(function(){??????document.getElementById('TheNewInput').focus();??
document.getElementById('TheNewInput').select();},?10);??
11,arguments.callee.length<script>
function test(x,y,z)
{
alert('argu.length:'+arguments.length);//實參的長度4
alert('argu.callee.length:'+arguments.callee.length);//形參的長度3
return x+y+z;
}
test(1,2,3,4);
</script>
12,js的substr和substring不一樣
"aaaaa".substr(N1,N2)??? 從指定的位置(N1)截取指定長度(N2)的字符串;
"aaaaa".substring(N1,N2) 從指定的位置(N1)到指定的位置(N2)的字符串;
舉個例子:
alert("123456789".substr(2,5)) 它顯示的是 "34567"
alert("123456789".substring(2,5)) 則顯示的為 "345"
13,js的switch也可用字符串匹配,只要能用==的都可以
function ? test(cs){ ?
?? switch(cs){ ?
? case ? "a":alert("input ? a");break; ?
? case ? "b":alert("input ? b");break; ?
? case ? "c":alert("input ? c");break; ?
? default:alert("input ? other ? words");break; ?
? } ?
? } ?
? test("a");
14,在js中動態生成代碼
eval("var obj = document." + formName + "." + filedname + ";");
15,使用Javascript的eval生成json對象有個地方要注意:
var? myJSONtext="{a1:'黃夏柳',a2:'賈政經'}";
var myObject = eval('(' + myJSONtext + ')');//即要括起來,同時用兩副單引號
用下面的寫法不行var myObject=eval("(' + myJSONtext + ')");不知何故。
16,登錄后用window.open實現瀏覽器全屏
//window.open(document.location, '窗體名稱', 'fullscreen');//這句打開并去掉所有欄,連任務欄都蓋住
//window.open(document.location,'窗體名稱','fullscreen,scrollbars');//帶滾動條
window.open('url','maxwindow','toolbar=no,location=no,directories=no,menubar=no,scrollbars=yes,resizable=no,status=no');
opener=null;//沒有這句,關閉時有提示,ie5.5以上有效。
//如果是跳轉到一個iframe,就改為parent.opener=null
window.close();//關閉當前窗口
17,
IE,FF下取Iframe window對象的方法IE:document.frames("Iframe_ID");
FF: document.frames("Iframe_ID") : document.getElementById("ifr_1").contentWindow;
IE和FF都兼容的方法:var ifr = document.frames ? document.frames("ifr_1") : document.getElementById("ifr_1").contentWindow;
18,關于右下角彈出窗口:
?? 參看鳳凰網、天涯或CSDN的例子
19,鏈接的點擊,兩種方式:
<A href="#" onclick="log();">
<a href="javascript:log()">? 千萬不要寫target="_parent",這樣調的是父頁面的函數