javascript控制表单提交后,提交按钮不可用,防止用户多次提交
例子:
<script type="text/javascript">
function sub() {
document.forms[0].elements[0].disabled = true;
document.forms[0].submit();
}
</script>
</head>
<body>
<form action="login.action" method="post">
<input type="button" name="btn" value="提交表单" οnclick="return sub();" />
</form>
</body>
javascript验证网址url的正则表达式
这个url的正则表达式判断的js!是比较全面的。它验证的情况包括!IP,域名(domain),ftp,二级域名,域名中的文件,域名加上端口!是我见过的最全面的url验证了!可能还有遗漏的!
例子:
<script language="JavaScript">
function IsURL(str_url){
var strRegex = "^((https|http|ftp|rtsp|mms)?://)"
+ "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@
+ "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184
+ "|" // 允许IP和DOMAIN(域名)
+ "([0-9a-z_!~*'()-]+\.)*" // 域名- www.
+ "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名
+ "[a-z]{2,6})" // first level domain- .com or .museum
+ "(:[0-9]{1,4})?" // 端口- :80
+ "((/?)|" // a slash isn't required if there is no file name
+ "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
var re=new RegExp(strRegex);
if (re.test(str_url)){
return (true);
}else{
return (false);
}
}
</script>
JavaScript获取选中文本 光标所在元素
例子:<script type="text/javascript"> function GetSelectedText (){ var userSelection, ta; if (window.getSelection && document.activeElement){ if (document.activeElement.nodeName == "TEXTAREA" || (document.activeElement.nodeName == "INPUT" && document.activeElement.getAttribute("type").toLowerCase() == "text")){ ta = document.activeElement; userSelection = ta.value.substring(ta.selectionStart, ta.selectionEnd); } else { userSelection = window.getSelection(); } document.getElementById("seltext").innerHTML += "Selection="+userSelection.toString()+" [1]<br>"; } else { // all browsers, except IE before version 9 if (document.getSelection){ userSelection = document.getSelection(); document.getElementById("seltext").innerHTML += "Selection="+userSelection.toString()+" [2]<br>"; } // IE below version 9 else if (document.selection){ userSelection = document.selection.createRange(); document.getElementById("seltext").innerHTML += "Selection="+userSelection.text+" [3]<br>"; } } } </script> </head> <body οnmοuseup="GetSelectedText()"> <h1>Drag to select text (script runs on mouseup)</h1> <textarea rows="5" cols="30">Select text or part of it.</textarea><br> <input type="text" name="text1" value="Some more play text" size="50"><br> <div id="seltext"></div> </body>