These days have not updated the log, this article is also considered some of their own experience to talk about. Simple to say some skills to everyone, I hope to help you, if you still have questions, you can leave a message below.
One, congruent operators In JS, a number that is not 0 is true except for a few special variables. For example: Undefined,null, "" and so on. This is, in most cases, what we expect.
But you should not ignore this congruent operator. Therefore, the need for a more rigorous, we can use the equality operator = = = See the following code:
Ordinary some of the efficacy
var foo = function (arg) {
if (!arg) {
Alert ("normal false");
else {alert ("true");}
}
Foo (null); False
Foo (false); False
Foo (""); False
Foo ("Never-online"); True
Full equivalence test
var foo = function (arg) {
if (Arg===false) {
Alert ("false");
else {alert (' not a false variable ')}
}
Foo (null); Not a false variable
Foo (false); False
Foo (""); Not a false variable
Foo ("Never-online"); Not a false variable
Second, one of the application of variables--removing web nesting For example, you want to embed baidu.com into one of your own iframe, and the result is not what you want, because it has a code
if (self!=top) {top.location=self.location;}
This code means: if the Web page has nested words (that is, the use of frame technology, will be removed nesting, and the frame in the URL to the top-level window)
This is a headache, for example, to use this code in some free space. What if it's removed? Use this code to get rid of nesting:
<script>
var location= ' Never-online ';
</script>
Here's how you can get rid of nesting with this code:
We know that the JS variable, if the variable is so
<script type= "Text/javascript" >
<! [cdata[
MyVar = "Never-online";
(function () {
var MyVar;
Alert (MyVar)
})()
]]>
</script>
Is the string that can display "Never-online". And when you//var myvar this sentence (that is, not commenting it out) The result is not the same, will show the undefined. We use the Var location= "Never-online" is the same reason, the following code may be for you resolving.
<script type= "Text/javascript" >
<! [cdata[
alert (location);
var location;
]]>
</script>
Similarly, the second run, the Var location this sentence to restore, compare it should understand the reason. The same is true of window,document. You can try if you are interested.
third, get the path of the current page There are a number of ways to do this, such as using the Location object, but the example I'm taking here is an IMG tag created with. Look at the code:
<script type= "Text/javascript" >
<! [cdata[
var p = document.createelement ("IMG");
P.src= "."; alert (P.SRC);
]]>
</script>
All we know, "." The current folder is represented in the path. Therefore, the use of IMG to get the path in some cases, you can quickly get the path.
four, use!! Operator gets a Boolean value of a variable, which, in a way, is equivalent to a forced transition Like what
<script type= "Text/javascript" >
<! [cdata[
var issupportedxmlhttp =!! New ActiveXObject ("MSXML2. XMLHTTP ");
Alert (issupportedxmlhttp)
]]>
</script>
Why is this, we can learn from the tip of the answer. No repetition here.
Five, conditional compilation. You can use this method if you feel it is necessary. This is similar to the conditional compilation in some strong languages. In many cases, it is used--compatible.
Here is a simple mention, if you are interested, you can refer to MSDN, or read some JS conditional compiled articles.
Only a few syntaxes are required:
@cc_on-This sentence is required to be written in conditional compilation, which indicates activation conditional compilation
@set @varname = Term-this sentence is assigned
Here's the verdict.
@if
@else
@end
A simple example:
/* @cc_on
document.write ("JScript version:" + @_jscript_version + ".<br>");
/* @if (@_jscript_version >= 5)
document.write ("JScript version 5.0+.<br \/>");
document.write ("Only when browsers support jscript5+ can you see these words .<br>");
@else @*/
document.write ("When you use other browsers (such as Firefox, IE 4.x, etc.) when you see this line of text <br>");
/* @end
@*/
Six, the other, now do not think so much, temporarily write here
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.