JavaScript Advanced Programming---Learning Notes (iv)

Source: Internet
Author: User

1. Global variables cannot be deleted through the delete operator, whereas properties defined directly on the Window object can.

    var age =;     = "Red";     Delete age; // Cannot delete    delete color; // can delete     alert (window.age); //  in    alert (Window.color); // undefined

2. Attempting to access an undeclared variable throws an error, but by querying the window object you can know if a variable that might not be declared exists.

    var newvalue = OldValue; // will throw the wrong, because OldValue is    undefined var newvalue = Window.oldvalue; // There is no error, because this is a property query, and the result of newvalue is undefined

3.location Object Query string parameters

Although Location.search can return everything from the question mark to the end of the URL, there is no way to access each of these query string parameters individually. To do this, you can create a function that parses the query string and then returns an object that contains all the parameters:

functionGetquerystringargs () {//get the query string and remove the question mark from the beginning    varQS = (location.search.length > 0? location.search.substring (1): ""); //objects that hold data    varargs = []; //get each item    varItems = qs.length? Qs.split ("&") : []; varitem =NULL; varName =NULL; varValue =NULL; //add each item individually to the Args object     for(vari=0;i< items.length;i++) {Item= Items[i].split ("="); Name= decodeURIComponent (item[0]); Value= decodeURIComponent (item[1]); if(name.length) {Args[name]=value; }    }    returnargs;}
Suppose the query string is? q=javascript&num=10
var args = Getquerystringargs ();
Alert (args["Q"]);//javascript
Alert (args["num"]);//10

The first step in this function is to first remove the question mark at the beginning of the query string, provided that the Location.search must contain one or more characters. All parameters are then saved in the Args object, which is created in literal form. Next, divide the query string by the number (&) and return an array of strings in the Name=value format. The For loop then iterates over the array, dividing each item according to the equals sign, returning the first item to the parameter name, and the second item as an array of parameter values. Then use Decodecomponent () to decode name and value separately (because the query string should be encoded), and then name as the attribute of the Args object and value as the corresponding property. Finally, the function is called, and each query string parameter is a property of the returned object, facilitating access to each parameter.

JavaScript Advanced Programming---Learning Notes (iv)

Related Article

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.