博客
 
« CSS对各个浏览器兼容问题整理分享一下:养花基本知识,今天买了第一盘花! »

网页javascript精华代码集

【1、普通的弹出窗口】
<script language=javascript>
<!--
window.open ('page.html')
-->
</script>
  <!-- 和 -->是对一些版本低的浏览器起作用,在这些老浏览器中不会将标签中的代码作为文本显示

出来。用单引号和双引号都可以,只是不要混用。

【2、经过设置后的弹出窗口】
<script language=javascript>
<!--
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=javascript> 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=javascript>
<!--
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 onload=openwin()>
.....
</body>
</html>

  这里定义了一个函数openwin(),怎么调用呢?

  方法一:<body onload=openwin()> 浏览器读页面时弹出窗口;
  方法二:<body onunload=openwin()> 浏览器离开页面时弹出窗口;
  方法三:用链接调用:        方法四:用按钮调用:


【4、同时弹出2个窗口】
  对源代码稍微改动一下:
<script language=javascript>
<!--
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)不要相同,或者干脆全部为空。

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

  如下代码加入主窗口<head>区:

<script language=javascript>
<!--
function openwin() {
window.open(page.html,,width=200,height=200)
}
//-->
</script>
加入<body>区:
<a href=1.htm onclick=openwin()>open</a>即可。

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

  下面我们再对弹出的窗口进行一些控制,效果就更好了。如果我们再将一小段代码加入弹出的页面(

注意是加入到page.html的html中,可不是主页面中,否则...),让它10秒后自动关闭是不是更酷了?

  首先,将如下代码加入page.html文件的<head>区:
<script language=javascript>

function closeit() {

settimeout(self.close(),10000) //毫秒

}

</script>
 然后,再用<body onload=closeit()> 这一句话代替page.html中原有的<body>这一句就可以了。(这

一句话千万不要忘记写啊!这一句的作用是调用关闭窗口的代码,10秒钟后就自行关闭该窗口。)

  【7、在弹出窗口中加上一个关闭按钮】
<form>
<input type='button' value='关闭' onclick='window.close()'>
</form>

  【8、内包含的弹出窗口-一个页面两个窗口】
通过下面的例子,你可以在一个页面内完成上面的效果。

<html>
<head>
<script language=javascript>
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=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=<input type=button onclick=openwin() value=打开窗口>
</body>
</html>

  看看 openwindow.document.write()里面的代码不就是标准的html吗?只要按照格式写更多的行即

可。千万注意多一个标签或少一个标签就会出现错误。记得用openwindow.document.close()结束啊。


【9、弹出的窗口之cookie控制】

  回想一下,上面的弹出窗口虽然酷,但是有一点小毛病。比如你将上面的脚本放在一个需要频繁经

过的页面里(例如首页),那么每次刷新这个页面,窗口都会弹出一次,是不是非常烦人?:-(
  解决办法:
  我们使用cookie来控制一下就可以了。首先,将如下代码加入主页面html的<head>区:

<script>
function openwin(){
window.open(page.html,,width=200,height=200)
}
function get_cookie(name) {
var search = name + =
var returnvalue = ;
if (documents.cookie.length > 0) {
offset = documents.cookie.indexof(search)
if (offset != -1) {
offset += search.length
end = documents.cookie.indexof(;, offset);
if (end == -1)
end = documents.cookie.length;
returnvalue=unescape(documents.cookie.substring(offset, end))
}
}
return returnvalue;
}

function loadpopup(){
if (get_cookie('popped')==''){
openwin()
documents.cookie=popped=yes
}
}

</script>

  然后,用<body onload=loadpopup()>(注意不是openwin而是loadpop啊!)替换主页面中原有的

<body>这一句即可。你可以试着刷新一下这个页面或重新进入该页面,窗口再也不会弹出了。真正的

pop-only-once!

  写到这里弹出窗口的制作和应用技巧基本上算是完成了。


  1.弹启一个全屏窗口

<html>
<body onload=window.open('http://www.pconline.com.cn','example01','fullscreen');>;
<b>www.e3i5.com</b>
</body>
</html>

  2.弹启一个被f11化后的窗口

<html>
<body onload=window.open(''http://www.pconline.com.cn','example02','channelmode');>;
<b>www.e3i5.com</b>
</body>
</html>

  3.弹启一个带有收藏链接工具栏的窗口

<html>
<body onload=window.open

('http://www.pconline.com.cn','example03','width=400,height=300,directories');>
<b>www.e3i5.com</b>
</body>
</html>

  4.网页对话框

<html>
<script language=javascript>
<!--
showmodaldialog

('http://www.pconline.com.cn','example04','dialogwidth:400px;dialogheight:300px;
dialogleft:200px;dialogtop:150px;center:yes;help:yes;resizable:yes;status:yes')
//-->
</script>
<b>www.e3i5.com</b>
</body>
</html>

<html>
<script language=javascript>
<!--
showmodelessdialog

('http://www.pconline.com.cn','example05','dialogwidth:400px;dialogheight:300px;
dialogleft:200px;dialogtop:150px;center:yes;help:yes;resizable:yes;status:yes')
//-->
</script>
<b> target=_blank>http://www.pconline.com.cn</b>
</body>
</html>

  showmodaldialog()与showmodelessdialog()的区别,在于showmodaldialog()打开模式窗口,

showmodelessdialog()打开无模式窗口。

dialogheight: iheight 设置对话框窗口的高度。
dialogwidth: iwidth 设置对话框窗口的宽度。   
dialogleft: ixpos 设置对话框窗口相对于桌面左上角的left位置。
dialogtop: iypos 设置对话框窗口相对于桌面左上角的top位置。
center: {yes no 1 0 } 指定是否将对话框在桌面上居中,默认值是“yes”。
help: {yes no 1 0 } 指定对话框窗口中是否显示上下文敏感的帮助图标。默认值是“yes”。   
resizable: {yes no 1 0 } 指定是否对话框窗口大小可变。默认值是“no”。
status: {yes no 1 0 } 指定对话框窗口是否显示状态栏。对于非模式对话框窗口,默认值是“yes”;

对于模式对话框窗口,默认值是 “no”。


网页经典代码
1. 将彻底屏蔽鼠标右键,无右键菜单
<body oncontextmenu=window.event.returnvalue=false>

也可以用于网页中table框架中
<table border oncontextmenu=return(false)><td>no</table>


2.取消选取、防止复制
<body onselectstart=return false>


3.不准粘贴
<body onpaste=return false>


4.防止复制
<body oncopy=return false; oncut=return false;>


5.ie地址栏前换成自己的图标
<link rel=shortcut icon href=favicon.ico>

说明:关于favicon.ico文件的制作。你可以先在fw中做一个图片,属于你自己站点一个小图标。然后在

acd see将文件属性改为*.ico,然后将你做的*.ico文件传到你的服务器目录中,然后就可以使用以上代

码来实现,当别人登陆你的站点时,地址栏里使用的就是你自定义的图标了。


6.可以在收藏夹中显示出你的图标
<link rel=bookmark href=favicon.ico>

说明:制作方法和上面的一样。只是显示的方式不同,这个是在别人收藏你的网页地址时显示的个性图

标。


7.关闭输入法
<input style=ime-mode:disabled>

说明:这段代码是在表格提交时用到的。也就是在输入数据时不可以使用其他输入法模式。


8.永远都会带着框架
<script language=javascript><!--
 if (window == top)top.location.href = frames.htm;// --></script>

说明:frames.htm为你的网页,这也是保护页面的一种方法


9.防止被人frame
<script language=javascript><!--
 if (top.location != self.location)top.location=self.location;
// --></script>


10.网页将不能被另存为
<noscript><iframe src=*.html></iframe></noscript>
说明:<noscirpt>的用法很广,其中一条就是可以使js广告失效。


11.查源文件
<input type=button value=查看网页源代码
onclick=window.location = 'view-source:'+ target=_blank>http://bbs.055.cn/test.htm

';>


12.cookie脚本记录,有很大的用处哦

function get_cookie(name) {
var search = name + =

var returnvalue = ;

if (documents.cookie.length > 0) {

offset = documents.cookie.indexof(search)

if (offset != -1) { // if cookie exists

offset += search.length

// set index of beginning of value

end = documents.cookie.indexof(;, offset);

// set index of end of cookie value

if (end == -1)

end = documents.cookie.length;

returnvalue=unescape(documents.cookie.substring(offset, end))

}

}

return returnvalue;

}


function loadpopup(){

if (get_cookie('popped')==''){

openpopup()

documents.cookie=popped=yes

}

}

说明:以上是js代码,请自己加起始符和结束符


13.内框架<iframe>使用
iframe标记的使用格式是:   

<iframe src=url width=x height=x scrolling=[option] frameborder=x
name=main></iframe>
src:文件的路径,既可是html文件,也可以是文本、asp等  
width、height:内部框架区域的宽与高;   
scrolling:当src的指定的html文件在指定的区域不显不完时,滚动选项,如果设置为no,则不出现滚动

条;如为auto:则自动出现滚动条;如为yes,则显示;  frameborder:区域边框的宽度,为了让“内

部框架“与邻近的内容相融合,常设置为0。
name:框架的名字,用来进行识别。
比如:  当你想用父框架控制内部框架时,可以使用: target=框架的名字来控制。

例子:<iframe name=mm src=http://bbs.055.cn;; width=100% height=100% marginwidth=0

marginheight= ... ot; frameborder=0 scrolling=no></iframe>

14.自动跳转
在源代码中的<head>…</head>加入如下代码:
<meta http-equiv=refreshcontent=3;url=http://bbs.055.cn; charset=gb2312>

说明:content=3 表示3秒刷新到url


15.如何改变链接的鼠标形状
只需在链接上加上这一代码就行的了
或者跟上面的用css写也行

style=cursor:hand       style=cursor:crosshair
style=cursor:text       style=cursor:wait
style=cursor:move       style=cursor:help
style=cursor:e-resize     
style=cursor:n-resize
style=cursor:nw-resize     style=cursor:w-resize
style=cursor:s-resize     
style=cursor:se-resize
style=cursor:sw-resize

以上代码你只需要加到连接或是页面的style区里就可以实现鼠标多样化。


16.全屏显示
<form>
<div align=center>
<input type=button name=fullscreen value=全屏显示 onclick=window.open(document.location,

'big', 'fullscreen=yes')>
</div>
</form>
把它放到<body>中。


17.设为首页

<script language=javascript>
<!--
function defaul_home(){
this.home.style.behavior='url(#default#homepage)';this.home.sethomepage(

http://bbs.055.cn/[/url]';
}
var focusok=false;
if (navigator.appname == netscape{
focusok=true;
}
vers=navigator.appversion;
if (navigator.appname == microsoft internet explorer{
pos=vers.lastindexof('.');
vers=vers.substring(pos-1,vers.length);
}
proper_version=parsefloat(vers);

if(proper_version>=5){
focusok=true;
}
function launchstock1(htmlurl){
var stock=window.open

(htmlurl,stock,top=2,left=2,toolbar=no,location=no,directories=no,status=no,menubar=no,scro

llbars=yes,
resizable=no,width=700,height=510;
if(focusok){
stock.focus();
}
return true;
}
function launchstock(){
var stock=window.open

(,stock,top=2,left=2,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=

yes,

resizable=no,width=700,height=510;
if(focusok){
stock.focus();
}
return true;
}
// -->
</script>
<a href=# name=home onclick=defaul_home(); title===e代时光==>设为首页</a>


18.这里是加入收藏夹的代码

<a href=# onclick=window.external.addfavorite(http://bbs.055.cn';.'拂晓雅阁')

target=_self title=拂晓雅阁>加入收藏夹</a>


19.flash图片效果
以下代码加入<head>区域
<script language=javascript>
<!--
function makevisible(cur,which){
if (which==0)
cur.filters.alpha.opacity=100
else
cur.filters.alpha.opacity=20
}

//-->

</script>
以下代码加入<body>区域
<img src=http://bbs.055.cn/images/logo.gif;; style=filte ... nbsp;onmouseover=makevisible

(this,0) onmouseout=makevisible(this,1) width=63 height=56> //图片地址请自己改


20.load 进度条

<table cellspacing=0 cellpadding=0 bgcolor=#ffffff width=40% id=p><tr><td>
<table cellspacing=0 cellpadding=0 bgcolor=#0000ff height=18

id=q><tr><td></td></tr></table></td></tr></table>
</center>
<script language=javascript>
var r = 0; load();
function load() {r = r + 2; q.style.width = r + %; time= settimeout(load(),50);
if (r > 100) {cleartimeout(time); p.style.width=0}}
</script>
27 全屏
<script language=javascript>
window.open('index.asp','','fullscreen=1');
</script>


21.背景图片滚动

<body scroll=no background=images/bg.jpg link=#00ff00 alink=#ff0000 vlink=#00ff00

bgcolor=#000080 topmargin=8>
<script language=javascript>
var backgroundoffset = 0;
var bgobject = eval('document.body');
function scrollbg(maxsize) {backgroundoffset = backgroundoffset + 1;
if (backgroundoffset > maxsize) backgroundoffset = 0;
bgobject.style.backgroundposition = 0  + backgroundoffset;}
var scrolltimer = window.setinterval(scrollbg(410), 20)
</script>


22.网页不会被缓存

html网页
<meta http-equiv=pragma content=no-cache>
<meta http-equiv=cache-control content=no-cache, must-revalidate>
<meta http-equiv=expires content=wed, 26 feb 1997 08:21:57 gmt>
或者<meta http-equiv=expires content=0>
asp网页
 response.expires = -1
 response.expiresabsolute = now() - 1
 response.cachecontrol = no-cache
php网页
header(expires: mon, 26 jul 1997 05:00:00 gmt;
header(cache-control: no-cache, must-revalidate;
header(pragma: no-cache;

23.最小化、最大化、关闭窗口
<object id=hh1 classid=clsiddb880a6-d8ff-11cf-9377-00aa003b7a11>
<param name=command value=minimize></object>
<object id=hh2 classid=clsiddb880a6-d8ff-11cf-9377-00aa003b7a11>
<param name=command value=maximize></object>
<object id=hh3 classid=clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11>
<param name=command value=close></object>
<input type=button value=最小化 onclick=hh1.click()>
<input type=button value=最大化 onclick=hh2.click()>
<input type=button value=关闭 onclick=hh3.click()>

24.判断上一页的来源

asp页:
request.servervariables(http_referer
java script:
document.referrer


25.光标是停在文本框文字的最后

<script language=javascript>
function cc()
{
 var e = event.srcelement;
 var r =e.createtextrange();
 r.movestart('character',e.value.length);
 r.collapse(true);
 r.select();
}
</script>
<input type=text name=text1 value=123 onfocus=cc()>


javascript几个表单常用的验证脚本:

     只能输入数字

<input onkeyup="value=value.replace(/[^\d]/g,'') "
       onbeforepaste="clipboarddata.setdata('text',clipboarddata.getdata('text').replace(/

[^\d]/g,''))">
  
 
     enter键可以让光标移到下一个输入框

<input onkeydown="if(event.keycode==13)event.keycode=9" > <br>

     只能是中文

<input onkeyup="value=value.replace(/[ -~]/g,'')" onkeydown="if(event.keycode==13)

event.keycode=9"> <br>

     屏蔽输入法

<input style="ime-mode:disabled" onkeydown="if(event.keycode==13)event.keycode=9"> <br>

     只能输入英文和数字

<input onkeyup="value=value.replace(/[\w]/g,'') "
       onbeforepaste="clipboarddata.setdata('text',clipboarddata.getdata('text').replace(/

[^\d]/g,''))"
       onkeydown="if(event.keycode==13)event.keycode=9"> <br>


     只能显示,不能修改

<input readonly value="只能显示,不能修改">
 
     禁止复制选择等................

<script language="javascript" type="text/javascript">

     双击鼠标滚动屏幕的代码
var currentpos,timer;
function initialize()
{
timer=setinterval ("scrollwindow ()",30);
}
function sc()
{
clearinterval(timer);
}
function scrollwindow()
{
currentpos=document.body.scrolltop;
window.scroll(0,++currentpos);
if (currentpos !=document.body.scrolltop)
sc();
}
document.onmousedown=sc
document.ondblclick=initialize

 

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

 
运营中的品牌网站:真人秀节目资讯网 我要出气网
Copyright 2008 © AliCui.com all right reserved. 沪ICP备07002205号 MSN:AliCui@MSN.Com
进入真人秀