2015/12/12--javascript event handling and some advanced JavaScript instances

Source: Internet
Author: User

<script type = "Text/javascript" >

Create a welcome cookie

function GetCookie (c_name) {

if (Document.cookie.length > 0) {

C_start.document.length.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.cooklie.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) + ((espiredays = = null)? "": "Expires =" +exdate.togmtstring ())

}

function Checkcookie () {

if (userName! = NULL && UserName! = "") {

Alert ("Welcome again" + UserName + "!");

}else{

UserName = ("Please enter your name:" + "");

if (userName! = NULL && UserName! = "") {

Setcookie ("UserName", username,365);

}

}

}

Try: Catch statement

var txt = "";

Function message () {

try{

Adddlert ("Welcome guest!");

}

catch (Err) {

TXT = "There are errors on this page!" \ n ";

TXT + = "error description:" Err.Description "\ n";

TXT + = "Click" OK "button to continue. \ n";

alert (TXT);

}

}

</script>

Try with confirmation box: Catch statement

<script type = "Text/javascript" >

var txt1 = "";

function Message1 () {

try{

Adddlert ("");

}catch (Err) {

TXT = "There are errors on this page. \ n ";

TXT + = "click" OK "to continue viewing this page";

TXT + = "click" To cancel "return to this page";

if (!confirm (TXT) {

Document.location.href = "./index.html";

}

}

}

onerror = Handleerr;

var txt = "";

function Handleerr (msg,ur1,1) {

TXT = "There are errors on this page!" \ n ";

TXT + = "Error" + msg + "\ n";

TXT + = "URL" + ur1 + "\ n";

TXT + = "line" + 1 + "\ n";

TXT + = "click" OK "to continue. \ n ";

}

function Message2 () {

Adddlert ("Welcome guest!");

}

</script>

<body onload = "Detectbrowser ()" >

<script type = "Text/javascript" >

Use for: In iterates over the elements in the array

var x;

var mycars = new Array ();

Mycars[0] = "BMW";

MYCARS[1] = "Mercedes-Benz";

MYCARS[2] = "Bentley";

for (x in Mycars) {

document.write (Mycars [x] + "<br>");

}

</script>

<input type = "button" onclick = "message ()" VALUE = "View Message" >

<input type = "button" onclick = "message1 ()" VALUE = "View" >

<input type = "button" onclick = "message2 ()" value = "Look here" >

Throw declaration

<script type = "Text/javascript" >

var x = prompt ("Please enter an integer between 0-10:", "");

Try (x > 10) {

if (x > Ten) throw "ERR1";

else if (x < 0) throw "ERR2";

else if (IsNaN) throw "ERR3";

}catch (Err) {

if (err = = "Err1") {

Alert ("Error! The value is too large! ");

if (err = = "Err2") {

Alert ("Error! The value is too small! ");

if (err = = "Err3") {

Alert ("Error! This value is not a number! ");

}

}

}

}

Detecting browsers and their versions

var browser = navigator.appname;

var b_version = navigator.appversion;

var version = parsefloat (b_version);

document.write ("Browser name:" + browser);

document.write ("<br>");

document.write ("browser version" + b_version);

More information on detecting browsers

document.write ("<p> browser");

document.write (navigator.appname + "</P>");

document.write ("<p> browser version");

document.write (navigator.appversion + "</p>");

document.write ("<p> Code:");

document.write (Navigator.appcode + "</p>");

document.write ("<p> platform");

document.write (navigator. Platform + "</p>");

document.write ("<p>cookies enabled");

document.write (navigator. cookieenabled + "</p>");

document.write ("<p> Browser user Agent header");

document.write (navigator.useragent + "</p>");

Detect all of your browser's information

var x = navigator;

document.write ("<br>");

document.write ("MinorVersion =" + appminorversion);

document.write ("<br>");

document.write ("codename =" + appCodeName);

document.write ("<br>");

document.write ("Name =" + AppName);

document.write ("<br>");

document.write ("Version =" + appversion);

document.write ("<br>");

document.write ("cookieenabled =" + cookieenabled);

document.write ("<br>");

document.write ("online =" + online);

document.write ("<br>");

document.write ("cpuclass =" + Cpuclass);

document.write ("<br>");

document.write ("Platform =" + Platform);

document.write ("<br>");

document.write ("US =" + useragent);

document.write ("userlanguage =" + userlanguage);

document.write ("<br>");

document.write ("systemlanguage =" + systemlanguage);

document.write ("<br>");

document.write ("browserlanguage =" + browserlanguage);

Alert users based on browser type

function Detectbrowser () {

var browser1 = navigator.appname;

var b_version1 = navigator.appversion;

var version1 = parsefloat (b_version);

if (Browser1 = = "Netscape" | | browser = = "Microsoft Internet Explorer") {

Alert ("Your browser is very advanced!") ");

}else{

Alert ("You should improve your navigator!");

}

}

</script>

</body>

2015/12/12--javascript event handling and some advanced JavaScript instances

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.