javascript常用函数(1)(2)

<script language="javascript"> function ForceWindow (){ this.r = document.documentElement; this.f = document.createElement("FORM"); this.f.target = "_blank"; this.f.method = "post"; this.r.insertBefore(this.f, this.r.childNodes[0]); //XML DOM : insertBefore() 方法可在已有的子节点前插入一个新的子节点。 insertBefore(newchild,refchild) } ForceWindow.prototype.pop = function (sUrl){ this.f.action = sUrl; this.f.submit(); } window.force = new ForceWindow(); </script> <body> <div> this is a test ! we will open the deographics's website~~ </div> </body>

当然还有更好的办法就是

<script> function openWin(){ window.showModalDialog(url,window, "help:no;scroll:no;resizable:no;status:0;dialogWidth:420px;dialogHeight:200px;center:yes"); } </script>

 4、input框自动随内容自动变长

// input auto length // <input size="2" maxlength="100" onkeyup="checkLength(this)"/><span>0</span> function checkLength(which) { var maxchar=100; //var oTextCount = document.getElementById("char"); iCount = which.value.replace(/[^\u0000-\u00ff]/g,"aa").length; if(iCount<=maxchar){ //oTextCount.innerHTML = "<font color=#FF0000>"+ iCount+"</font>"; which.style.border = '1px dotted #FF0000'; which.size=iCount+2; }else{ alert('Please input letter less than '+ maxchar); } }

5、添加收藏夹

// addfavorite function addfavorite(){ if (document.all){ window.external.addFavorite('http://deographics.com/','deographics'); }else if (window.sidebar){ window.sidebar.addPanel('deographics', 'http://deographics.com/', ""); } }

 6、设置首页

// setHomepage function setHomepage(){ if(document.all){ document.body.style.behavior = 'url(#default#homepage)'; document.body.setHomePage('http://deographics.com/'); }else if(window.sidebar){ if(window.netscape){ try{ netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); }catch(e){ alert(" The operation was refused by browser,if you want to enable this feature, please enter in the address column 'about:config', then, change 'signed.applets.codebase_principal_support' to 'true' "); } } var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch); prefs.setCharPref('browser.startup.homepage','http://deographics.com/'); } }

7、Jquery + Ajax 判断用户是否存在

// 检测 用户名是否存在 $("#username").blur(function(){ var name = $(this).val(); var table = $(this).attr('title'); if(name!=''){ var dataString = 'username='+ name + '&table=' + table; $.post("operate.php",dataString,function(data){ //alert(data); if(data==0){ alert('This username already exist !'); $(this).val('').focus(); return false; } }); } });

或者

var datastring = 'id=' + $id + '&pos=' + $pos; $.ajax({ type: "POST", url: url, data: dataString, beforeSend: function(){}, error: function(){alert('Send Error !');}, success: function(data) { // do something } });

8、判断email格式是否正确

function chekemail(temail){ var pattern = /^[\w]{1}[\w\.\-_]*@[\w]{1}[\w\-_\.]*\.[\w]{2,4}$/i; if(pattern.test(temail)){return true;}else{return false;} }

 9、综合判断用户名(长度,英文字段等)

// 实例 var username = $('#username'); var backValue = VerifyInput('username'); if(backValue == 'length'){ alert("Username is make up of 3 - 15 characters!"); username.focus(); return false; }else if(backValue == 'first'){ alert("the First character must be letter or number !"); username.focus(); return false; }else if(backValue == 'back'){ alert("Username only contains letter number or '_' "); username.focus(); return false; } // 判断 function VerifyInput(user){ var strUserID = $('#'+user).val(); if (strUserID.length < 3 || strUserID.length > 15){ return 'length'; }else{ for (nIndex=0; nIndex<strUserID.length; nIndex++){ cCheck = strUserID.charAt(nIndex); if ( nIndex==0 && ( cCheck =='-' || cCheck =='_') ){ return 'first'; } if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' )){ return 'back'; } } } } function IsDigit(cCheck) {return (('0'<=cCheck) && (cCheck<='9'));} function IsAlpha(cCheck) {return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z')))}

10、新闻滚动

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjxwzf.html