var obj = new Object();
obj.triggerOnce = function(fn) { //Control the function to trigger only once
return function() {
try {
fn.apply(this, arguments);
}
catch (e) {
var txt = "There was an error on this page.nn";
txt = "Error message: " e.message "nn";
txt = "Error name: " e.name "nn ";
//alert(txt);//This line may need to be commented out on the official platform
}
finally {
fn = null;
}
}
}
Call form:
function showMsg(arg){
alert(arg);
}
var fn=obj.triggerOnce(showMsg);
fn(1);//Execute only once
fn( 2);//fn is not called
Finally, regarding the apply method of javascript, you can simply refer to this old article.
【External article】Use javascript to obtain the value of querystring
In common websites, we often encounter URLs in the form of "XXXX.aspx?username=jeffwong&address=Beijing", through the Request of asp.net It is easy to get the key value of the object, and it is not difficult to use js. You can use the following function:
Code
var urlStrs = location.search; //QueryString
var Request = {};
Request.Count = 0;
Request.Add = function (name, value) {
if (Request.hasOwnProperty(name) == false) {
Request.Count; //If it does not exist, add 1 to the count
}
Request[name] = value;
return true;
}
Request.QueryString = function(name) {//Get the corresponding key value
var key = name.toString().toLocaleLowerCase(); //No Case sensitive
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]); //unescape decoding
this.Add(name, value); //Add
}
}
}
The test function is as follows:
Code
/ /Test function
function test() {
Request.Init(urlStrs); //Initialize 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 more commonly used unescape for decoding. In fact, regarding the URL parameter encoding, there are usually escape, encodeURIComponent and encodeURI. Of these three encoding functions, escape is a built-in function of the window object, while the other two are built-in functions of JavaScript. Their decoding functions also have a one-to-one correspondence (you can refer to this article). The corresponding inversion functions are respectively are unescape, decodeURIComponent and decodeURI.
Author: Jeff Wong