Home > Web Front-end > JS Tutorial > Some skills you need to know in JS (DHTML)_Basic knowledge

Some skills you need to know in JS (DHTML)_Basic knowledge

WBOY
Release: 2016-05-16 19:21:43
Original
1129 people have browsed it

There are no update logs these days, so this article can be regarded as talking about some of my own experiences. I will briefly share some tips with you. I hope it will be helpful to you. If you still have any questions, you can leave a message below.

1. Congruence operator
In JS, all numbers that are not 0 are true, except for a few special variables. For example: undefined, null, "", etc. This is what we expect in most cases.
But you should not ignore this equality operator. Therefore, if more stringent effects are needed, we can use the congruence operator ===, see the following code:
//Some common effects
var foo = function(arg) {
if ( !arg) {
alert("normal false");
} else { alert("true"); }
}
foo(null); //false
foo(false ); //false
foo(""); //false
foo("never-online"); //true
//Congruent effect
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

2. One of the applications of variables - removing web page nesting
For example, if you want to embed baidu.com into an iframe of your own, the result you get is not What you want, because it has such a code
if(self!=top){top.location=self.location;}
The meaning of this code is: if the web page is nested (that is, If frame technology is used, the nesting will be removed and the URL in the frame will be given to the top-level window)
This is a headache. For example, the above code is also used in some free spaces. What if it is removed? Use this code to remove nesting:


The following explains why using this code can remove nesting:
We know that in JS variables, if the variable is like this


can display the string "never-online". And when you restore the sentence //var myVar (that is, when you don't comment it out) the result is different and undefined will be displayed. The same is true when we use var location="never-online". The following code may solve your doubts.

Similarly, when running for the second time, restore the var location sentence, and you should understand the reason after comparing it. The same is true for windows and documents. If you are interested, you can try it.

3. Get the path of the current page

To achieve this goal, there are many ways, such as using the location object, but the example I give here is to use an IMG tag created . Look at the code:
As we know, "." in the path represents the current folder. Therefore, using IMG to obtain the path can obtain the path faster in some cases.


4. Use the !! operator to get the boolean value of a variable. To a certain extent, it is equivalent to forced transformation

For example,
Why is this happening? We can get the answer from Tip 1. It will not be repeated here.


5. Conditional compilation.

You can use this method if you feel it is necessary. This is similar to conditional compilation in some strong languages. In many cases it is used for - compatibility.
This is just a brief mention. If you are interested, you can refer to MSDN or read some articles about JS conditional compilation.
Only a few syntaxes are needed:
@cc_on - This sentence is required in conditional compilation, indicating that conditional compilation is activated
@set @varname = term - This sentence is for assignment
Here below It’s just a matter of judgment
@if
@else
@end
Simple example:
/*@cc_on
document.write("JScript version: " @_jscript_version ".
");
/*@if (@_jscript_version >= 5)
document.write("JScript version 5.0 .
");
document.write("Only if the browser supports You can only see these texts when using JScript5.
");
@else @*/
document.write("When you use other browsers (such as: Firefox, IE 4.x, etc.) When I saw this line of text <script> <BR>var location='never-online'; <BR></script>"); <script> <BR> //<![CDATA[ <BR> myVar = "never-online"; <BR> (function () { <BR> //var myVar; <BR> alert(myVar) <BR> })() <BR> //]]> <BR> </script> /*@end <script> <BR> //<![CDATA[ <BR> alert(location); <BR> //var location; <BR> //]]> <BR> </script>@*/ <script> <BR> //<![CDATA[ <BR> var p = document.createElement("IMG"); <BR> p.src="."; alert(p.src); <BR> //]]> <BR> </script><script> <BR> //<![CDATA[ <BR> var isSupportedXMLHttp = !!new ActiveXObject("MSXML2.XMLHTTP"); <BR> alert(isSupportedXMLHttp) <BR> //]]> <BR> </script> 6. Others, I can’t think of so many right now, so I’ll just write them here for now.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template