高級JavaScriptの実例02

3921 ワード

歓迎クッキーを作成します.
<html>
<head>
<script type="text/javascript">
function getCookie(c_name)
{
if (document.cookie.length>0)
{ 
c_start=document.cookie.indexOf(c_name + "=")
if (c_start!=-1)
{ 
c_start=c_start + c_name.length+1 
c_end=document.cookie.indexOf(";",c_start)
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end))
} 
}
return ""
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : "; expires="+exdate.toGMTString())
}

function checkCookie()
{
username=getCookie('username')
if (username!=null && username!="")
  {alert('Welcome again '+username+'!')}
else 
  {
  username=prompt('Please enter your name:',"")
  if (username!=null && username!="")
    {
    setCookie('username',username,365)
    }
  }
}
</script>
</head>
<body onLoad="checkCookie()">
</body>
</html>
ボタンアニメーション
<html>
<head>
<script type="text/javascript">
function mouseOver()
{
document.b1.src ="../i/eg_mouse.jpg"/*tpa=http://www.w3school.com.cn/i/eg_mouse.jpg*/
}
function mouseOut()
{
document.b1.src ="../i/eg_mouse2.jpg"/*tpa=http://www.w3school.com.cn/i/eg_mouse2.jpg*/
}
</script>
</head>

<body>
<a href="../index.html" tppabs="http://www.w3school.com.cn/index.html" target="_blank">
<img border="0" alt="Visit W3School!" src="../i/eg_mouse2.jpg" tppabs="http://www.w3school.com.cn/i/eg_mouse2.jpg" name="b1"  onmouseover="mouseOver()" onmouseout="mouseOut()" /></a>
</body>
</html>
JavaScriptのイメージマップを追加しました.
<html>
<head>
<script type="text/javascript">
function writeText(txt)
{
document.getElementById("desc").innerHTML=txt
}
</script>
</head>

<body>
<img src="../i/eg_planets.jpg" tppabs="http://www.w3school.com.cn/i/eg_planets.jpg" border="0" usemap="#planetmap" alt="Planets" />

<map name="planetmap" id="planetmap">

<area shape="circle" coords="180,139,14"
onMouseOver="writeText('   20    60   ,               ,             ,                。')"
href ="../example/html/venus.html" tppabs="http://www.w3school.com.cn/example/html/venus.html" target ="_blank" alt="Venus" />

<area shape="circle" coords="129,161,10"
onMouseOver="writeText('            ,               。')"
href ="../example/html/mercur.html" tppabs="http://www.w3school.com.cn/example/html/mercur.html" target ="_blank" alt="Mercury" />

<area shape="rect" coords="0,0,110,260"
onMouseOver="writeText('                             。')"
href ="../example/html/sun.html" tppabs="http://www.w3school.com.cn/example/html/sun.html" target ="_blank" alt="Sun" />

</map>

<p id="desc"></p>

</body>
</html>