MDA/MDD/TDD/DDD/DDDDDDD
posts - 536, comments - 111, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

js笔记

Posted on 2008-03-12 21:10 leekiang 阅读(406) 评论(0)  编辑  收藏 所属分类: web开发
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.blogjava.net/vls/archive/2008/05/11/199808.html

5,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",这样调的是父页面的函数



只有注册用户登录后才能发表评论。


网站导航: