var toy = (monkey_behavior=="good") ? "videogames" : "rocks";类似语句,减少代码使容易理解
2、要使JavaScript中各种同名变量不会发生混淆,你可以在声明变量时在变量前面加上var。在一个函数内用var声明后灯的变量叫做局部变量,它只存在于该函数内部。通常情况下你应该尽量使用局部变量。
这里是用var声明后的正确的JavaScript代码:
function fahrenToCelsius(faren){ var temp = (faren - 32) * 5 / 9; return temp; } function convertTemp(){ var temp = prompt("what temperature Fahrenheit? ","50"); var celsius = badFahrenToCelsius(temp); alert(temp + " degrees Fahrenheit is " + celsius + " degrees Celsius."); }
3、字符串处理
javascript字符串处理函数 http://blog.csdn.net/banmuhuangci/archive/2005/12/23/559985.aspx
Js字符串操作函数大全 http://www.phpweblog.net/kiyone/archive/2007/04/19/1135.aspx
4、简单的学习数组的例子
var phone_book = new Array(); phone_book["happy"] = "(203) 555-1234"; phone_book["sleepy"] = "(203) 555-2345"; phone_book["sneezy"] = "(203) 555-4321"; phone_book["sleazy"] = "(203) 555-3245"; phone_book["sneery"] = "(203) 555-3213"; phone_book["bleary"] = "(203) 555-2365"; phone_book["tweaked"] = "(203)555-1664";
表单的代码如下:
指定的JS函数
function displayNumber(phone_book, entry){ var the_number = phone_book[entry]; window.document.the_form.number_box.value = the_number; }
5、cookies学习
cookies局限性在于:不是每个人的浏览器都欢迎cookies.即便是用户的浏览器欢迎cookies,但用户也有可能拒绝cookies的访问(大部分人还是欢迎的)每个域名只分配20个cookies,所以要节省着什么它们.Cookies不得大于4 KB,当然4,000字节的容量是足够的了.
设置一个基本的cookie很容易.你所需做的只是在一个cookie_name=value表单中生成一个字符串,然后设置document.cookie属性.唯一的技巧:cookie值中不能有空格,逗号或分号.好在你无需担心这些问题,因为有一系列的函数可以帮你对cookies属性编码和解码:
(1)、保存一个cookie:
function setCookie(){ var the_name = prompt("What's your name?",""); var the_cookie = "wm_javascript=username:" + escape(the_name); document.cookie = the_cookie; alert("Thanks, now go to the next page."); }
(2)、读取cookie范例的代码:
function readCookie(){ var the_cookie = document.cookie; var broken_cookie = the_cookie.split(":"); var the_name = broken_cookie[1]; var the_name = unescape(the_name); alert("Your name is: " + the_name); }
(3)、读取和编写多重cookies:
编写的代码很简单如下:
var the_cookie ="my_happy_cookie=happiness_and_joy"; document.cookie = the_cookie; var another_cookie= "my_other_cookie=more_joy_more_happiness"; document.cookie = another_cookie;
读取的代码如下:
function WM_readCookie(name){ if(document.cookie == '') return false; return unescape(WM_getCookieValue(name)); } function WM_getCookieValue(name){ var firstChar,lastChar; var theBigCookie = document.cookie; firstChar = theBigCookie.indexOf(name); if(firstChar != -1){ firstChar += name.length + 1; lastChar = theBigCookie.indexOf(';', firstChar); if(lastChar == -1) lastChar = theBigCookie.length; return theBigCookie.substring(firstChar, lastChar); } else{ return false; } }
(4)、设置cookie失效的时间
function setCookie(){ var the_name = prompt("What's your name?",""); var the_date = new Date("December 31, 2023"); var the_cookie_date =the_date.toGMTString(); var the_cookie = "my_cookie=" + escape(the_name); the_cookie = the_cookie +";expires=" + the_cookie_date; document.cookie = the_cookie; }
(5)、cookie路径和域
路径"path"用于设置可以读取一个cookie的最顶层的目录.将cookie的路径设置为你的网页最顶层的目录可以让该该目录下的所有网页都能访问该cookie.
function setCookie(){ var the_name = prompt("What's your name?",""); var the_cookie = "cookie_puss=" + escape(the_name) + ";" ; var the_cookie = the_cookie + "path=/;"; var the_cookie = the_cookie + "domain=webmonkey.com;"; document.cookie = the_cookie; }
6、定时的用法
(1)、设置定时的功能
其关键指令是setTimeout()和clearTimeout()方法。利用setTimeout(), 指令可以在未来
的某个指定时间执行特定指令。如果你改变主意,你可以用clearTimeout()取消setTimeout的定时
var the_timeout = setTimeout("alertAndRedirect ();",3000); function alertAndRedirect(){ alert('ok! exhale!'); window.location.replace("timing.htm"); }
(2)、定时循环
var the_count = 0; var the_timeout; function doTimer(){ window.document.timer_form.the_text.value = the_count; the_count += 2; the_timeout = setTimeout("doTimer();", 2000); }
表单部分的代码如下:
(3)、复杂一点的定时例子
function writeTime() { var today = new Date(); var hours = today.getHours(); var minutes = today.getMinutes(); var seconds = today.getSeconds(); minutes = fixTime(minutes); seconds = fixTime(seconds); var the_time = hours + ":" + minutes + ":" + seconds; window.document.the_form.the_text.value = the_time; //每半秒钟执行一次该函数 the_timeout= setTimeout('writeTime();',500); } function fixTime(the_time) { if (the_time <10) { the_time = "0" + the_time; } return the_time; }
或者下面的例子:
function alertInAMinute(){ var the_string = "hello"; the_timeout = setTimeout("alert(" + the_string + ");",60000); }