日期:2014-05-16  浏览次数:20347 次

js 重新学习笔记( 三 ) 特殊字符 , try catch

1, ? ? js 捕捉错误 ?

用 try .... catch?


noerror ?事件 , 老式的捕获错误的处理方式.

? ? ? ? ? ?注意:chrome、opera 和 safari 浏览器不支持 onerror 事件。


2, ? ?throw( exception ) ?


? ? ? ? exception 可以是 字符串, 数值 ,逻辑值 , 或对象


?

? var x=prompt("Enter a number between 0 and 10:","")

        try
        { 
            if(x>10) 
            throw "Err1"
            else if(x<0)
            throw "Err2"
        } 
        catch(er)
        {
             if(er=="Err1") 
                   alert("Error! The value is too high")
             if(er == "Err2") 
                   alert("Error! The value is too low") 
	}



3, ?noerror (chrome, opera , safari)



4, ?特殊字符?


? ? ?js 用反斜杠 在文本字符串中插入 ?省略号, 换行号, 和其他特殊字符?


? ? ?var we = "we are so called / "vikied"/ in north " ;


? ? ?现在 JavaScript 就可以输出正确的文本字符串了:We are the so-called "Vikings" from the north。


? ??document.write ("You \& me are singing!")?

?


? ? 上面的例子会产生以下输出:

 You & me are singing!



?下面的表格列出了其余的特殊字符,这些特殊字符都可以使用反斜杠来添加到文本字符串中:



下面的表格列出了其余的特殊字符,这些特殊字符都可以使用反斜杠来添加到文本字符串中: