In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use the global function of JavaScript. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
What are the global functions of JavaScript? The function describes decodeURI () to decode an encoded URI. DecodeURIComponent () decodes an encoded URI component. EncodeURI () encodes the string as URI. EncodeURIComponent () encodes the string as a URI component. Escape () encodes the string. Eval () evaluates the JavaScript string and executes it as script code. IsFinite () checks whether a value is a finite number. IsNaN () checks whether a value is a number. Number () converts the value of the object to a number. ParseFloat () parses a string and returns a floating point number. ParseInt () parses a string and returns an integer. String () converts the value of the object to a string. Unescape () decodes the string encoded by escape (). Second, the detailed explanation of JavaScript global function? 2.1.Eval () 2.1.1. Example one
First, take a look at the example:
Eval ("Xerox 10; [xss_clean] (Xeroy)"); [xss_clean] ("
"+ eval (" 2x2 "); [xss_clean] ("
"+ eval (Xbox 17))
Results:
two hundred
four
twenty-seven
Special usage {}:
[xss_clean] ("
"+ eval {334}))
At this time, the return result is: 6 We find that the difference between {} and () is actually the same:
/ / {} / 2 this writing is not supported [xss_clean] ("
"+ eval {30003} / 2)); / / () is OK [xss_clean] ("
"+ eval (3x3) / 2); / / if {} also wants to do this kind of calculation, you can also do the following: [xss_clean] ("
"+ eval {(3x3) / 2})); 2.1.2. Example 2
Take a look at the results returned by eval () in other cases:
Eval ("2x3") / / returns 5var myeval = eval; / / EvalError exception myeval ("2fol3") may be thrown; / / EvalError exception may be thrown
You can use the following code to detect whether the parameter of eval () is legal:
Try {alert ("Result:" + eval (prompt ("Enter an expression:", "));} catch (exception) {alert (exception);} 2.1.3. Example 3 (parsing JSON strings) 2.1.3.1.eval parsing function:
JSON does not allow functions to be included, but you can store functions as strings and then convert strings to functions.
Var text ='{"name": "Runoob", "alexa": "function () {return 10000;}", "site": "www.runoob.com"}'; var obj = JSON.parse (text); obj.alexa = eval ("(" + obj.alexa + ")"); document.getElementById ("demo") [xss_clean] = obj.name + "Alexa Rank:" + obj.alexa () Two methods for converting 2.1.3.2.JSON strings into objects / / methods for converting JSON strings into JS objects-var obj = JSON.parse ('{"name": "runoob", "alexa": 10000, "site": "www.runoob.com"}'); [xss_clean] (obj.name + "") / / method 2 for converting JSON strings to JS objects / / string var test1 ='{"name": "qlq", "age": 25}'; var obj2 = eval ("(" + test1 + ")"); / / must be parenthesized [xss_clean] (obj2.name + "" + obj2.age)
Results:
Runoob
Qlq
twenty-five
Why add eval ("(" + test1 + ")") / / "here for eval?
The reason is: the problem of eval itself. Because json starts and ends with "{}", it is treated as a statement block in JS, so it must be converted into an expression forcefully.
The purpose of adding parentheses is to force the eval function to force the expression in parentheses (expression) to be converted into an object instead of being executed as a statement (statement) when processing JavaScript code. For example, for example, the literal amount of the object {}, if you do not add the outer braces, then eval will recognize the curly braces as the opening and ending tags of the JavaScript code block, then {} will be considered to execute an empty statement. So the following two execution results are different:
Alert (eval ("{}"); / / return undefinedalert (eval ("({})"); / / return object [Object]
This way of writing can be seen everywhere in JS.
Such as: (function ()) {} (); when doing closure operation, etc.
Alert (dataObj.root.length); / / output the number of root sub-objects $.each (dataObj.root,fucntion (idx,item) {if (idx==0) {return true;} / / output the name and value alert of each root sub-object ("name:" + item.name+ ", value:" + item.value);})
Note: for general js to generate json objects, you just need to replace the $.each () method with a for statement, and everything else remains the same.
2.1.3.3. For the JSON string returned by the server, if the jquery asynchronous request sets type (usually for this configuration property) to "json", or uses the $.getJSON () method to get the server return, then the eval () method is not needed, because the result is already a json object, just call the object directly Here, take the $.getJSON method as an example to illustrate the data processing method: $.getJSON ("data returned here by http://www.phpzixue.cn/",{param:"gaoyusi"},function(data){// is already a json object / / the following other operations are the same as in the first case $.each (data.root,function (idx,item) {if (idx==0) {return true)" / / same as countinue, return false with break} alert ("name:" + item.name+ ", value:" + item.value);});})
It is particularly important to note that the eval () method in Mode 1 dynamically executes the string (which may be a js script), which can easily cause security problems for the system. So you can use some third-party client script libraries that circumvent eval (). For example, JSON in JavaScript provides a script library of no more than 3k.
2.1.3.4. Add: the key of JSON parsed by eval () may not have "".
General JSON key must be in double quotation marks, which is similar to {"key": "vslue"}, but if the string is parsed to JSON in the form of eval ("(" + json+ ")"), json can be written as {key: "value"}.
2.2.decodeURI () and decodeURIComponent ()-Decoding function
DecodeURI () can decode the URI encoded by the encodeURI () function
Such as:
Const aaa ='# $¥% 23cccplink 'console.log (encodeURI (aaa)); / / # $% 20%EF%BF%A5%2523ccc/ console.log (decodeURI (aaa)); / / # $¥% 23ccc/ console.log (encodeURIComponent (aaa)); / /% 23%24%20%EF%BF%A5%2523ccc%2F console.log (decodeURIComponent (aaa)); / / # $¥# ccc/
When we obtain the address bar parameters, they are usually encapsulated into the following functions:
Export function getQueryObject (url) {url = url | | _ window.location.href const search = url.substring (url.lastIndexOf ('?) + 1) const obj = {} const reg = / ([^? & =] +) = ([^ & =] *) / g search.replace (reg, (rs, $1) 2) = > {const name = decodeURIComponent ($1) let val = decodeURIComponent ($2) val = String (val) obj [name] = val return rs}) return obj} 2.3.encodeURI () and encodeURIComponent ()-Encoding function
EncodeURI ():
Grammar
EncodeURI (URIstring)
Parameter description
URIstring is required. A string containing URI or other text to be encoded.
Return value
A copy of the URIstring in which some characters will be replaced by hexadecimal escape sequences.
Description
This method does not encode ASCII letters and numbers, nor does it encode these ASCII punctuation marks:-_. ! ~ * ().
The purpose of this method is to fully encode URI, so the encodeURI () function does not escape the following ASCII punctuation marks with special meaning in URI:; /?: @ & = + $, #
EncodeURIComponent ():
Grammar
EncodeURIComponent (URIstring)
Parameter description
URIstring is required. A string containing the URI component or other text to be encoded.
Return value
A copy of the URIstring in which some characters will be replaced by hexadecimal escape sequences.
Description
This method does not encode ASCII letters and numbers, nor does it encode these ASCII punctuation marks:-_. ! ~ * ().
Other characters (such as:; /?
This is the end of the article on "how to use the global function of JavaScript". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 263
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.