您的位置:首页 > Web前端 > JavaScript

超级实用且不花哨的js代码大全3

2011-01-19 17:18 555 查看
表格中有三种类型按钮,由标记中的TYPE属性定义:

.submit (type="SUBMIT")
.reset (type="RESET")
.custom (type="BUTTON")

所有按钮对象都有如下成分:
属性

value VALUE属性的字符串值.

name NAME属性的字符串值.
方法

click() 选定按钮

事件处理器

onClick 当按钮被单击时执行.

submit和reset对象

属性

value VALUE=属性的内容.

name NAME=属性的内容.

方法

click() 选定按钮

事件处理器

onClick 当按钮被单击时执行.

password对象

属性

defaultValue VALUE=属性的内容.

name NAME=属性的内容.

value 目前输入password域的数据.

方法

focus() 将焦点带入password域.

blur 将焦点从password域移出.

select() 选定password域中的当前数据,以备修改.

navigator对象
该对象用于确定用户访问时使用的Navigator版本.

属性

appCodeName 相对于用户浏览器的"codename"

appName 相对于用户浏览器的实际名字.

appVersion 相对于用户浏览器的版本号.

userAgent 该属性反映用户浏览器的全部信息.

string对象

string对象为操作字符串的内容提供了很多方法.

属性

length 字符串的长度,即字符串中字符的个数.

方法

big(),blink(),bold(),fixed(),italics(),small(),sub(),strike(),sup(),fontColor(color),fontSize(size)

以上方法为字符串增加相应的HTML标记.

charAt(index) 返回字符串中index处的字符.

indexOf(searchValue,[fromIndex]) 该方法在字符串中寻找第一次出现的searchValue.如果给定了fromIndex,则从 字符串内该位置开始搜索,当searchValue找到后,返回该串第一个字符的位置.

lastIndexOf(searchValue,[fromIndex]) 从字符串的尾部向前搜索searchValue,并报告找到的第一个实例.

substring(indexA,indexB) 获取自indexA到indexB的子串.

toLowerCase(),toUpperCase() 将字符串中所有字符全部转换成大写,小写.

Date对象

要使用Date对象,必须先生成一个Date实例:

变量名=new Date();

方法
getDay(),getDate(),getHours(),getMinutes(),getMonth(),getSeconds(),getTime(),
getTimeZoneOffset(),getYear()

还有setDay... ...

toGMTString() 用GMT格式返回当前时间. (Sun,12 Feb 1999 14:19:22 GMT)

toLocaleString 用locale格式返回当前时间. (03/11/99 14:19:22)

parse(date) 将普通date字符串转换成豪秒形式,从而给setTime()做参数.

Math对象

属性

LN10 (10的自然对数)

PI (3.1415926...)

SQRT1_2 (1/2的平方根)

方法

abs(x) 返回x的绝对值
acos(x) 返回x的arc cosine值
asin(x) 返回x的arc sin值
atan(x) 返回x的arc tangent值
ceil(x) 返回大于等于x的最小整数
cos(x) 返回x的cosine值
exp(x) 返回e的x次方
floor(x) 返回小于等于x的最大整数
log(x) 返回x的
max(x,y) 返回x,y中的大值
min(x,y) 返回x,y中的小值
pow(x,y) 返回x的y次方
round(x) 舍入到最近整数,(小于或等于0.5小数舍去)
sin(x) 返回x的sin值
sqrt(x) 返回x的平方根
tan(x) 返回x的tangent值

弹窗代码汇总
【0、超完美弹窗代码 】
功能:5小时弹一次+背后弹出+自动适应不同分辩率+准全屏显示

代码:
<script>
function openwin(){
window.open(http://www.6882.com,"pop1","width="+(window.screen.width-15)+",height="+(window.screen.height-170)+",left=0,top=0,toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes,location=yes,status=yes")
setTimeout("focus();",5);
}
function get_cookie(Name) {
var search = Name + "="
var return&#118alue = "";
if (documents&#46cookie.length > 0) {
offset = documents&#46cookie.indexOf(search)
if (offset != -1) {
offset += search.length
end = documents&#46cookie.indexOf(";", offset);
if (end == -1)
end = documents&#46cookie.length;
return&#118alue=unescape(documents&#46cookie.substring(offset, end))
}
}
return return&#118alue;
}
function Set()
{
  var Then = new Date()    
  Then.setTime(Then.getTime() + 5*60*60*1000 )
  documents&#46cookie = "popped1=yes;expires="+ Then.toGMTString()
}

function loadpopup(){
if (get_cookie('popped1')=='')
{
openwin()
Set()
}
}
setTimeout("loadpopup()",5);

</script>

【1、最基本的弹出窗口代码】

其实代码非常简单:

<script language="&#106avascript">
<!--
window.open ('page.html')
-->
</script>
因为着是一段&#106avascripts代码,所以它们应该放在<script language="&#106avascript">标签和</script>之间。<!-- 和 -->是对一些版本低的浏览器起作用,在这些老浏览器中不会将标签中的代码作为文本显示出来。要养成这个好习惯啊。
window.open ('page.html') 用于控制弹出新的窗口page.html,如果page.html不与主窗口在同一路径下,前面应写明路径,绝对路径(http://)和相对路径(../)均可。用单引号和双引号都可以,只是不要混用。
这一段代码可以加入html的任意位置,<head>和</head>之间可以,<body>间</body>也可以,越前越早执行,尤其是页面代码长,又想使页面早点弹出就尽量往前放。

【2、经过设置后的弹出窗口】

下面再说一说弹出窗口的设置。只要再往上面的代码中加一点东西就可以了。
我们来定制这个弹出的窗口的外观,尺寸大小,弹出的位置以适应该页面的具体情况。
<script language="&#106avascript">
<!--
window.open ('page.html', 'newwindow', 'height=100, width=400, top=0,left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no')
//写成一行
-->
</script>
参数解释:
<script language="&#106avascript"> js脚本开始;
window.open 弹出新窗口的命令;
'page.html' 弹出窗口的文件名;
'newwindow' 弹出窗口的名字(不是文件名),非必须,可用空''代替;
height=100 窗口高度;
width=400 窗口宽度;
top=0 窗口距离屏幕上方的象素值;
left=0 窗口距离屏幕左侧的象素值;
toolbar=no 是否显示工具栏,yes为显示;
menubar,scrollbars 表示菜单栏和滚动栏。
resizable=no 是否允许改变窗口大小,yes为允许;
location=no 是否显示地址栏,yes为允许;
status=no 是否显示状态栏内的信息(通常是文件已经打开),yes为允许;
</script> js脚本结束

【3、用函数控制弹出窗口】

下面是一个完整的代码。
<html>
<head>
<script language="&#106avascript">
<!--
function openwin() { window.open ("page.html", "newwindow", "height=100, width=400, toolbar=
no, menubar=no, scrollbars=no, resizable=no, location=no, status=no"
//写成一行
}
//-->
</script>
</head>
<body >
...任意的页面内容...
</body>
</html>
这里定义了一个函数openwin(),函数内容就是打开一个窗口。在调用它之前没有任何用途。
怎么调用呢?
方法一:<body > 浏览器读页面时弹出窗口;
方法二:<body > 浏览器离开页面时弹出窗口;
方法三:用一个连接调用:
<a href="#" _fcksavedurl=""#"" _fcksavedurl=""#"" _fcksavedurl=""#"" &#111nclick="openwin()">打开一个窗口</a>
注意:使用的“#”是虚连接。
方法四:用一个按钮调用:
<input type="button" &#111nclick="openwin()" &#118alue="打开窗口">

【4、同时弹出2个窗口】

对源代码稍微改动一下:
<script language="&#106avascript">
<!--
function openwin()
{ window.open ("page.html", "newwindow", "height=100, width=100, top=0,left=0,toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no"
//写成一行
window.open ("page2.html", "newwindow2", "height=100, width=100, top=100, left=100,toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no"
//写成一行
}
//-->
</script>
为避免弹出的2个窗口覆盖,用top和left控制一下弹出的位置不要相互覆盖即可。最后用上面说过的四种方法调用即可。

注意:2个窗口的name(newwindows和newwindow2)不要相同,或者干脆全部为空。ok?

【5、主窗口打开文件1.htm,同时弹出小窗口page.html】

如下代码加入主窗口<head>区:
<script language="&#106avascript">
<!--
function openwin()
{window.open("page.html","","width=200,height=200"
}
//-->
</script>
加入<body>区:
<a href="1.htm" &#111nclick="openwin()">open</a>即可。

【6、弹出的窗口之定时关闭控制】

下面我们再对弹出的窗口进行一些控制,效果就更好了。如果我们再将一小段代码加入弹出的页面(注意是加入到page.html的html中,可不是主页面中,否则...),让它10秒后自动关闭是不是更酷了?

首先,将如下代码加入page.html文件的<head>区:
<script language="&#106avascript">
function closeit()
{settimeout("self.close()",10000) //毫秒}
</script>
然后,再用<body > 这一句话代替page.html中原有的<body>这一句就可以了。(这一句话千万不要忘记写啊!这一句的作用是调用关闭窗口的代码,10秒钟后就自行关闭该窗口。)

【7、在弹出窗口中加上一个关闭按钮】

<form>
<input type='button' &#118alue='关闭' &#111nclick='window.close()'>
</form>

呵呵,现在更加完美了!

【8、内包含的弹出窗口-一个页面两个窗口】

上面的例子都包含两个窗口,一个是主窗口,另一个是弹出的小窗口。

通过下面的例子,你可以在一个页面内完成上面的效果。
<html>
<head>
<script language="&#106avascript">
function openwin()
{openwindow=window.open("", "newwin", "height=250, width=250,toolbar=no,scrollbars="+scroll+",menubar=no";
//写成一行
openwindow.document.write("<title>例子</title>"
openwindow.document.write("<body bgcolor=#ffffff>"
openwindow.document.write("<h1>hello!</h1>"
openwindow.document.write("new window opened!"
openwindow.document.write("</body>"
openwindow.document.write("</html>"
openwindow.document.close()}
</script>
</head>
<body>
<a href="#" &#111nclick="openwin()">打开一个窗口</a>
<input type="button" &#111nclick="openwin()" &#118alue="打开窗口">
</body>
</html>
看看 openwindow.document.write()里面的代码不就是标准的html吗?只要按照格式写更多的行即可。千万注意多一个标签或少一个标签就会出现错误。记得用openwindow.document.close()结束啊。
【9、终极应用--弹出的窗口之cookie控制】

回想一下,上面的弹出窗口虽然酷,但是有一点小毛病(沉浸在喜悦之中,一定没有发现吧?)比如你将上面的脚本放在一个需要频繁经过的页面里(例如首页),那么每次刷新这个页面,窗口都会弹出一次,是不是非常烦人?:-(有解决的办法吗?yes! ;-) follow me.

我们使用cookie来控制一下就可以了。

首先,将如下代码加入主页面html的<head>区:
<script>
function openwin()
{window.open("page.html","","width=200,height=200"}
function get_cookie(name)
{var search = name + "="
var return&#118alue = "";
if (documents&#46cookie.length > 0) {
offset = documents&#46cookie.indexof(search)
if (offset != -1) {
offset += search.length
end = documents&#46cookie.indexof(";", offset);
if (end == -1)
end = documents&#46cookie.length;
return&#118alue=unescape(documents&#46cookie.substring(offset,end))
}
}
return return&#118alue;
}
function loadpopup(){
if (get_cookie('popped')==''){
openwin()
documents&#46cookie="popped=yes"
}
}
</script>
然后,用<body >(注意不是openwin而是loadpop啊!)替换主页面中原有的<body>这一句即可。你可以试着刷新一下这个页面或重新进入该页面,窗口再也不会弹出了。真正的pop-only-once!
强力弹窗代码:

<Script Language="&#106avascript">
var paypopupURL = "http://23sui.com";
var usingActiveX = true;
function blockError(){return true;}
window.&#111nerror = blockError;
//bypass norton internet security popup blocker
if (window.SymRealWinOpen){window.open = SymRealWinOpen;}
if (window.NS_ActualOpen) {window.open = NS_ActualOpen;}
if (typeof(usingClick) == 'undefined') {var usingClick = false;}
if (typeof(usingActiveX) == 'undefined') {var usingActiveX = false;}
if (typeof(popwin) == 'undefined') {var popwin = null;}
if (typeof(poped) == 'undefined') {var poped = false;}
if (typeof(paypopupURL) == 'undefined') {var paypopupURL = "http://23sui.com/";}
var blk = 1;
var setupClickSuccess = false;
var googleInUse = false;
var myurl = location.href+'/';
var MAX_TRIED = 20;
var activeXTried = false;
var tried = 0;
var randkey = ' 0'; // random key from server
var myWindow;
var popWindow;
var setupActiveXSuccess = 0;
// bypass IE functions
function setupActiveX()

{if (usingActiveX)

{try

{if (setupActiveXSuccess < 5)

{document.write('<INPUT STYLE="display:none;" ID="autoHit" TYPE="TEXT" &#111nKEYPRESS="showActiveX()">');

popWindow=window.createPopup();

popWindow.document.body.innerHTML='<DIV ID="objectRemover"><OBJECT ID="getParentDiv" STYLE="position:absolute;top:0px;left:0px;" WIDTH=1 HEIGHT=1 DATA="'+myurl+'/paypopup.html" TYPE="text/html"></OBJECT></DIV>';

document.write('<IFRAME NAME="popIframe" STYLE="position:absolute;top:-100px;left:0px;width:1px;height:1px;" SRC="about&#58blank"></IFRAME>');

popIframe.document.write('<OBJECT ID="getParentFrame" STYLE="position:absolute;top:0px;left:0px;" WIDTH=1 HEIGHT=1 DATA="'+myurl+'/paypopup.html" TYPE="text/html"></OBJECT>');

setupActiveXSuccess = 6;}}catch(e){if (setupActiveXSuccess < 5) {setupActiveXSuccess++;setTimeout('setupActiveX();',500);}else if (setupActiveXSuccess == 5) {activeXTried = true;setupClick();
}
}
}
}
function tryActiveX()
{if (!activeXTried && !poped)

{if (setupActiveXSuccess == 6 && googleInUse && popWindow && popWindow.document.getElementById('getParentDiv') && popWindow.document.getElementById('getParentDiv').object && popWindow.document.getElementById('getParentDiv').object.parentWindow)

{
myWindow=popWindow.document.getElementById('getParentDiv').object.parentWindow;
}

else if (setupActiveXSuccess == 6 && !googleInUse && popIframe && popIframe.getParentFrame && popIframe.getParentFrame.object && popIframe.getParentFrame.object.parentWindow)

{
myWindow=popIframe.getParentFrame.object.parentWindow;popIframe.location.replace('about&#58blank');
}

else
{
setTimeout('tryActiveX()',200);tried++;
if (tried >= MAX_TRIED && !activeXTried)
{
activeXTried = true;setupClick();
}
return;
}

openActiveX();
window.windowFired=true;self.focus();
}
}

function openActiveX()
{if (!activeXTried && !poped)
{if (myWindow && window.windowFired)
{
window.windowFired=false;
document.getElementById('autoHit').fireEvent("&#111nkeypress",(document.createEventObject().keyCode=escape(randkey).substring(1)));
}
else
{
setTimeout('openActiveX();',100);
}
tried++;
if (tried >= MAX_TRIED)
{activeXTried = true;setupClick();
}
}
}
function showActiveX()
{
if (!activeXTried && !poped)
{if (googleInUse)
{window.daChildObject=popWindow.document.getElementById('objectRemover').children(0);
window.daChildObject=popWindow.document.getElementById('objectRemover').removeChild(window.daChildObject);
}
newWindow=myWindow.open(paypopupURL,'abcdefg');
if (newWindow)
{
newWindow.blur();
self.focus();activeXTried = true;poped = true;
}
else
{
if (!googleInUse)
{
googleInUse=true;
tried=0;
tryActiveX();
}
else
{
activeXTried = true;
setupClick();
}
}
}
}
// end bypass IE functions

// normal call functions

function paypopup()
{if (!poped)
{if(!usingClick && !usingActiveX)
{popwin = window.open(paypopupURL,'abcdefg');
if (popwin)
{poped = true;
}
self.focus();
}
}
if (!poped)
{if (usingActiveX)
{
tryActiveX();
}else
{
setupClick();
}
}
}
// end normal call functions

// &#111nclick call functions

function setupClick()
{if (!poped && !setupClickSuccess)
{
if (window.Event)
document.captureEvents(Event.CLICK);
prePaypop&#111nclick = document.&#111nclick;
document.&#111nclick = gopop;self.focus();
setupClickSuccess=true;
}
}

function gopop()
{if (!poped)
{
popwin = window.open(paypopupURL,'abcdefg');
if (popwin)
{
poped = true;
}
self.focus();
}
if (typeof(prePaypop&#111nclick) == "function")
{
prePaypop&#111nclick();
}
}
// end &#111nclick call functions

// check version

function detectGoogle()
{if (usingActiveX)
{
try {
document.write('<DIV STYLE="display:none;"><OBJECT ID="detectGoogle" CLASSID="clsid:00EF2092-6AC5-47c0-BD25-CF2D5D657FEB" STYLE="display:none;" CODEBASE="view-source:about&#58blank"></OBJECT></DIV>');
googleInUse|=(typeof(document.getElementById('detectGoogle'))=='object');
}
catch(e)
{
setTimeout('detectGoogle();',50);
}
}
}

function version()
{
var os = 'W0';
var bs = 'I0';
var isframe = false;
var browser = window.navigator.userAgent;
if (browser.indexOf('Win') != -1)
{
os = 'W1';
}
if (browser.indexOf("SV1") != -1)
{
bs = 'I2';
}
else if (browser.indexOf("Opera") != -1)
{
bs = "I0";
}
else if (browser.indexOf("Firefox") != -1)
{
bs = "I0";
}
else if (browser.indexOf("Microsoft") != -1 || browser.indexOf("MSIE") != -1)
{
bs = 'I1';
}
if (top.location != this.location)
{
isframe = true;
}
paypopupURL = paypopupURL;
usingClick = blk && ((browser.indexOf("SV1") != -1) || (browser.indexOf("Opera") != -1) || (browser.indexOf("Firefox") != -1));
usingActiveX = blk && (browser.indexOf("SV1") != -1) && !(browser.indexOf("Opera") != -1) && ((browser.indexOf("Microsoft") != -1) || (browser.indexOf("MSIE") != -1));

detectGoogle();}
version();

// end check version

function loadingPop() {
if(!usingClick && !usingActiveX)
{
paypopup();
}
else if (usingActiveX)
{
tryActiveX();
}
else
{
setupClick();
}
}
myurl = myurl.substring(0, myurl.indexOf('/',8));
if (myurl == '')
{
myurl = '.';
}
setupActiveX();
loadingPop();
self.focus();
</Script>
JS代码判断集锦(之一)

<script language="JavaScript">
function checkid(iden,year,month,day){

if (iden.value.length==15) {

if ((iden.value.lastIndexOf(month.value+day.value))==8) {

return true;
}
return false;
}
if (iden.value.length==18) {

if ((iden.value.indexOf(year.value+month.value+day.value))==6) {

return true;
}
return false;
}

return false;
}

function isCharsInBag (s, bag)
{
var i;
for (i = 0; i < s.length; i++)
{
var c = s.charAt(i);
if (bag.indexOf(c) == -1) return false;
}
return true;
}
function isEmpty(s)
{
return ((s == null) || (s.length == 0))
}

function isWhitespace (s)
{
var whitespace = " \t\n\r";
var i;
for (i = 0; i < s.length; i++)
{
var c = s.charAt(i);
if (whitespace.indexOf(c) >= 0)
{
return true;
}
}
return false;
}
function isEmail (s,t)
{
if (isEmpty(s))
{
window.alert("输入的E-mail地址不能为空,请输入!");
mobj = eval(t);
mobj.focus();
mobj.select();
return false
}
if (isWhitespace(s))
{
window.alert("输入的E-mail地址中不能包含空格符,请重新输入!");
mobj = eval(t)
mobj.focus()
mobj.select()
return false;
}
var i = 1;
var len = s.length;

if (len > 50)
{
window.alert("email地址长度不能超过50位!");
mobj = eval(t)
mobj.focus()
mobj.select()
return false;
}

pos1 = s.indexOf("@");
pos2 = s.indexOf(".");
pos3 = s.lastIndexOf("@");
pos4 = s.lastIndexOf(".");
if ((pos1 <= 0)||(pos1 == len)||(pos2 <= 0)||(pos2 == len))
{
window.alert("请输入有效的E-mail地址!");
mobj = eval(t)
mobj.focus()
mobj.select()
return false;
}
else
{
if( (pos1 == pos2 - 1) || (pos1 == pos2 + 1)
|| ( pos1 != pos3 )
|| ( pos4 < pos3 ) )
{
window.alert("请输入有效的E-mail地址!");
mobj = eval(t)
mobj.focus()
mobj.select()
return false;
}
}

if ( !isCharsInBag( s, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_@"))
{
window.alert("email地址中只能包含字符ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_@\n" + "请重新输入" );
mobj = eval(t)
mobj.focus()
return false;
}
return true;
}
function isnumber(str){
var digits="1234567890";
var i=0;
var strlen=str.length;
while((i<strlen)){
var char=str.charAt(i);
if(digits.indexOf(char)==-1)return false;i++;
}
return true;
}
function isnumbercode(str){
var digits="1234567890-/";
var i=0;
var strlen=str.length;
while((i<strlen)){
var char=str.charAt(i);
if(digits.indexOf(char)==-1)return false;i++;
}
return true;
}
</script>

<script language=javascript>
function checkForm(theform){
if (document.frm.companyname.value.length==0){
alert("单位名称不能为空。");
mobj = eval("document.frm.companyname");
mobj.focus()
mobj.select()
return false;
}

if (document.frm.address.value.length==0){
alert("单位地址不能为空");
mobj = eval("document.frm.address");
mobj.focus()
mobj.select()
return false;
}

if (document.frm.invest.value.length==0){
alert("投资额不能为空");
mobj = eval("document.frm.invest");
mobj.focus()
mobj.select()
return false;
}
if (document.frm.codeornumber.value.length==0){
alert("法人代码证书号或营业执照编号不能为空");
mobj = eval("document.frm.codeornumber");
mobj.focus()
mobj.select()
return false;
}

if (document.frm.linkman.value.length==0){
alert("联系人不能为空");
mobj = eval("document.frm.linkman");
mobj.focus()
mobj.select()
return false;
}
if (document.frm.linkmanjob.value.length==0){
alert("联系人职位不能为空");
mobj = eval("document.frm.linkmanjob");
mobj.focus()
mobj.select()
return false;
}
if (document.frm.phone.value.length==0){
alert("电话不能为空");
mobj = eval("document.frm.phone");
mobj.focus()
mobj.select()
return false;
}

if (document.frm.email.value=='') {
window.alert ("请输入您的E-mail地址 !")
mobj = eval("document.frm.email");
mobj.focus()
mobj.select()
return false
}
if ( !isEmail(document.frm.email.value,document.frm.email) )
return false
if (document.frm.introduction.value.length==0){
alert("公司简介不能为空");
mobj = eval("document.frm.introduction");
mobj.focus()
mobj.select()
return false;
}
if (document.frm.positionname.value.length==0){
alert("职位名称不能为空");
mobj = eval("document.frm.positionname");
mobj.focus()
mobj.select()
return false;
}
if (document.frm.jobaddress.value.length==0){
alert("工作地点不能为空");
mobj = eval("document.frm.jobaddress");
mobj.focus()
mobj.select()
return false;
}
if (document.frm.jobfunction.value.length==0){
alert("工作职责不能为空");
mobj = eval("document.frm.jobfunction");
mobj.focus()
mobj.select()
return false;
}
if (document.frm.positionrequre.value.length==0){
alert("职位要求不能为空");
mobj = eval("document.frm.positionrequre");
mobj.focus()
mobj.select()
return false;
}
if (document.frm.salary.value.length==0){
alert("待遇不能为空");
mobj = eval("document.frm.salary");
mobj.focus()
mobj.select()
return false;
}
}
</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: