Use apply to allow javascript Functions to execute code only once _ javascript skills

Source: Internet
Author: User
Tags hasownproperty
Sometimes we only want to execute some step functions once to complete the task. How can this function be implemented? Simply imitate the following section to make it easy. The Code is as follows:


Var obj = new Object ();
Obj. triggerOnce = function (fn) {// control the function to be triggered only once
Return function (){
Try {
Fn. apply (this, arguments );
}
Catch (e ){
Var txt = "There was an error on this page. \ n ";
Txt + = "Error message:" + e. message + "\ n ";
Txt + = "Error name:" + e. name + "\ n ";
// Alert (txt); // comment out this line on the official Platform
}
Finally {
Fn = null;
}
}
}


Call form:

The Code is as follows:


Function showMsg (arg ){
Alert (arg );
}
Var fn = obj. triggerOnce (showMsg );
Fn (1); // only run once
Fn (2); // fn is not called


Finally, you can refer to the old article for the javascript apply method.
[Additional] use javascript to obtain the querystring Value
In common websites, "XXXX. aspx? Username = jeffwong & address = Beijing ". It is easy to get the key value through the Request object of asp.net, and it is not difficult to get the key value through js. You can use the following function to solve the problem:
Code

The Code is as follows:


Var urlStrs = location. search; // QueryString
Var Request = {};
Request. Count = 0;
Request. Add = function (name, value ){
If (Request. hasOwnProperty (name) = false ){
Request. Count ++; // if not, add 1 to the Count
}
Request [name] = value;
Return true;
}
Request. QueryString = function (name) {// obtain the corresponding key value
Var key = name. toString (). toLocaleLowerCase (); // case insensitive
Var result = null;
Try {
// If (Request. hasOwnProperty (key )){
Result = Request [key];
//}
}
Catch (e ){
// Alert (e. message );
}
Return result;
}
Request. Init = function (urlQueryString ){
If (urlQueryString. indexOf ("? ")! =-1 ){
Var url = urlQueryString. substr (1)
Var strArr = url. split ("&");
For (var I = 0; I <strArr. length; I ++ ){
Var strChildArr = strArr [I]. split ("= ");
Var name = String (strChildArr [0]). toLowerCase ();
Var value = unescape (strChildArr [1]); // scape Decoding
This. Add (name, value); // Add
}
}
}


The test function is as follows:
Code

The Code is as follows:


// Test the Function
Function test (){
Request. Init (urlStrs); // initialize the request
Alert (Request. Count );
Var name = "username ";
Var value = Request. QueryString (name );
Alert (Request [name]);
Alert (Request. QueryString (name ));
Name = "address"
Alert (Request [name]);
Alert (Request. QueryString (name ));
Name = "age ";
Alert (Request [name]);
Alert (Request. QueryString (name ));
}


It should be noted that the parameters in the url are usually encoded. This article uses the commonly used unescape for decoding. In fact, the url parameter encoding usually involves escape, encodeURIComponent and encodeURI encoding functions. escape is a built-in function of the window object, while the other two are built-in functions of javascript, their decoded functions also have a one-to-one correspondence relationship (you can refer to this article). Their Respective inversion functions are unescape, decodeURIComponent, and decodeURI.
Author: Jeff Wong
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.