Bobby

聚沙成塔 集腋成裘
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

JS学习贴

Posted on 2006-04-05 18:04  Bobby  阅读(632)  评论(0编辑  收藏  举报

JavaScript从入门到精通程序练习帖子-P92 2006年4月5日

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="JavaScript" type="text/javascript">
function showResults()
{
    
var resultMsg=""                    //数组方式读取控件名称
    if(document.survey.age[0].checked) resultMsg+="under 30"
    
if(document.survey.age[1].checked) resultMsg+="between 30 and 60"
    
if(document.survey.age[2].checked) resultMsg+="over 60"
    
if(document.survey.sex[0].checked) resultMsg+="male"
    
if(document.survey.sex[1].checked) resultMsg+="female"
    
if(document.survey.reading.checked) resultMsg+="reading"
    
if(document.survey.eating.checked) resultMsg+="eating"
    
if(document.survey.sleeping.checked) resultMsg+="sleeping"
    document.survey.results.value
=resultMsg    //赋值给text
}

    
function upperCaseResults()
    
{    //转化成大写
        var newResults = document.survey.results.value
        document.survey.results.value 
= newResults.toUpperCase()
    }

</script>
</head>
<body bgcolor="#FFFFFF">
<form name="survey" id="survey">
  Age:
  
<input type="radio" name="age" onclick="showResults()" />
  under 30 
&nbsp;
  
<input type="radio" name="age" onclick="showResults()" />
  between 30 and 60
&nbsp;
  
<input type="radio" name="age" onclick="showResults()" />
  over 60
  
<p> Sex:
    
<input type="radio" name="sex" onclick="showResults()" />
    Male
&nbsp;
    
<input type="radio" name="sex" onclick="showResults()" />
    Female
&nbsp; </p>
  
<p> Interests:
    
<input type="checkbox" name="reading" onclick="showResults()" />
    Reading
    
<input type="checkbox" name="eating" onclick="showResults()"/>
    Eating
    
<input type="checkbox" name="sleeping" onclick="showResults()"/>
    Sleeping 
</p>
  
<p>
    
<input type="button" name="MakeUpper"  value="ToUpperCase"  onclick="upperCaseResults()"/>
    
<input type="text" name="results" size="50" />
    
<input type="submit" name="submit" value="submit" onclick='return confirm("sure")' />
    
<input type="reset" name="reset" onclick='return confirm("Sure")' />
  
</p>
</form>
</body>
</html>

JavaScript从入门到精通程序练习帖子-P134 2006年4月10日

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
    
function setTimer()
    
{
        timer
=window.setTimeout("alert('Too slow!')",1000)
    }

    
function clearTimer()
    
{
        clearTimer(timer)  
//堆寨溢出在第13行
        alert("Congradulations!")
    }

</script>
</head>
<body onload="setTimer()">
<form>
  
<input type="button" value="点击" onclick="clearTimer()" />
</form>
</body>
</html>


实际应用
经常上网的朋友可能会到过这样一些网站,一进入首页立刻会弹出一个窗口,

或者按一个连接或按钮弹出,通常在这个窗口里会显示一些注意事项、版权信息

、警告、欢迎光顾之类的话或者作者想要特别提示的信息。其实制作这样的页面

效果非常的容易,只要往该页面的HTML里加入几段Javascript代码即可实现。下

面俺就带您剖析它的奥秘。
【1、最基本的弹出窗口代码】
其实代码非常简单:
<SCRIPT LANGUAGE="javascript">
<!--
window.open ('page.html')
-->
</SCRIPT>
因为着是一段javascripts代码,所以它们应该放在<SCRIPT LANGUAGE="javascr

ipt">标签和</script>之间。<!-- 和 -->是对一些版本低的浏览器起作用,在这

些老浏览器中不会将标签中的代码作为文本显示出来。要养成这个好习惯啊。
window.open ('page.html') 用于控制弹出新的窗口page.html,如果page.html

不与主窗口在同一路径下,前面应写明路径,绝对路径(http://)和相对路径(..

/)均可。
用单引号和双引号都可以,只是不要混用。
这一段代码可以加入HTML的任意位置,<head>和</head>之间可以,<body>间</b

ody>也可以,越前越早执行,尤其是页面代码长,又想使页面早点弹出就尽量往

前放。

【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=n

o, 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()"> 浏览器离开页面时弹出窗口;
方法三:用一个连接调用:
<a href="#" onclick="openwin()">打开一个窗口</a>
注意:使用的“#”是虚连接。
方法四:用一个按钮调用:
<input type="button" onclick="openwin()" value="打开窗口">

【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=n

o, status=no")
//写成一行
window.open ("page2.html", "newwindow2", "height=100, width=100, top=1

00, left=100,toolbar=no, menubar=no, scrollbars=no, resizable=no, loca

tion=no, status=no")
//写成一行
}
//-->
</script>
为避免弹出的2个窗口覆盖,用top和left控制一下弹出的位置不要相互覆盖即可

。最后用上面说过的四种方法调用即可。
注意:2个窗口的name(newwindows和newwindow2)不要相同,或者干脆全部为空。

OK?

【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中原有的<BO

DY>这一句就可以了。(这一句话千万不要忘记写啊!这一句的作用是调用关闭窗

口的代码,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=#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="#" onclick="openwin()">打开一个窗口</a>
<input type="button" onclick="openwin()" value="打开窗口">
</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 returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) {
offset += search.length
end = document.cookie.indexOf(";", offset);
if (end == -1)
end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

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

</script>

然后,用<body onload="loadpopup()">(注意不是openwin而是loadpop啊!)

替换主页面中原有的<BODY>这一句即可。你可以试着刷新一下这个页面或重新进

入该页面,窗口再也不会弹出了。真正的Pop-Only-Once!

JavaScript从入门到精通程序练习帖子-P145 2006年4月14日13:29:26

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<script language="javascript">
function createSummary()
{
    win2 
= open("","window2")                                  //弹出窗口
    win2.document.open("text/plain")
    win2.document.writeln(
"Links:"+document.links.length)       //输出页面连接的数量
    win2.document.writeln("Anchors:"+document.anchors.length)  
    win2.document.writeln(
"Forms:"+document.forms.length)       //输出页面表单的数量
    win2.document.writeln("Images:"+document.applets.length)
    win2.document.writeln(
"Embeds:"+document.embeds.length)    
    
//Embeds 物件是一個陣列, 它代表著所有在 document 內用 <embed> 標記寫成的元件, <embed> 最常用來在網頁播放音樂和電影。
    win2.document.close
}

</script>
<body>
<name="#top"></a>
<p><href="http;//www.jaworski.com/javascript"></a>
    
<href="http://www.sybex.com/"></a>
<form>
<input type="button" name="help" value="help" onclick="alert('Click one')"
</form
>
<script language="javascript">
    setTimeout(
"createSummary()",5000)  //延时5秒后在执行函数
</script>
</body>
</html>