You need to know some tips in JS (DHTML)

Source: Internet
Author: User

I haven't updated the log these days. This article is my own experience. I would like to give you some tips. If you have any questions, please leave a message below.

I. Equality Operators
In JS, the numbers not 0 are all true, except for a few special variables. For example, undefined, null, and. This is what we expect in most cases.
However, you should not ignore this full equality operator. Therefore, we need to strictly verify this. We can use the = full equality operator to view the following code:
// Some common verification results
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 equivalent Verification
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. Application of Variable -- Remove Web page nesting
For example, if you want to embed baidu.com into your own iframe, the result is not what you want, because it has such a code
If (self! = Top) {top. location = self. location ;}
This Code indicates that if a webpage is nested (that is, frame technology is used, nesting is removed and the url in the frame is given to the top-level window)
This is a headache. For example, the above Code is also used in some free space. What if it is removed? With this code, nesting can be removed:
<Script>
Var location = 'Never-online ';
</Script>
The following describes why this code can be used to remove nesting:
We know that in JS variables, if the variables are like this
<Script type = "text/javascript">
// <! [CDATA [
MyVar = "never-online ";
(Function (){
// Var myVar;
Alert (myVar)
})()
//]>
</Script>
Yes, the string "never-online" can be displayed. When you restore the // var myVar Statement (that is, if you do not comment it out), the result is different and undefined is displayed. The same is true when we use var location = "never-online". The following code may be used to answer your questions.
<Script type = "text/javascript">
// <! [CDATA [
Alert (location );
// Var location;
//]>
</Script>
Similarly, in the second run, we should restore the var location statement and compare it to understand the cause. Similarly, this is true for Windows and documents. If you are interested, you can try it.

3. Obtain the path of the current page.
There are many methods to achieve this, such as using the location object, but the example here uses the created IMG tag. Check the Code:
<Script type = "text/javascript">
// <! [CDATA [
Var p = document. createElement ("IMG ");
P. src = "."; alert (p. src );
//]>
</Script>
As we know, "." indicates the current folder in the path. Therefore, IMG can be used to obtain a path quickly in some cases.

4. Use it !! To some extent, the operator obtains the boolean value of a variable, which is equivalent to forced transformation.
For example
<Script type = "text/javascript">
// <! [CDATA [
Var isSupportedXMLHttp = !! New ActiveXObject ("MSXML2.XMLHTTP ");
Alert (isSupportedXMLHttp)
//]>
</Script>
Why? We can learn the answer from tip 1. This is not repeated here.

5. Conditional compilation.
You can use this method if you think it is necessary. This is similar to Conditional compilation in some strong languages. In many cases, it is used for -- compatibility.
Here is just a brief introduction. If you are interested, refer to MSDN or some JS Conditional compilation articles.
Only a few syntaxes are required:
@ Cc_on-this statement is required in Conditional compilation, indicating that Conditional compilation is activated.
@ Set @ varname = term-this sentence is assigned a value.
Here is the judgment.
@ If
@ Else
@ End
Simple Example:
/* @ Cc_on
Document. write ("JScript version:" + @ _ jscript_version + ". <br> ");
/* @ If (@ _ jscript_version> = 5)
Document. write ("JScript version 5.0 +. <br \/> ");
Document. write ("You can see the text only when the browser supports JScript5 +. <br> ");
@ Else @*/
Document. write ("You can see this line of text when using other browsers (such as Firefox, IE 4.x) <br> ");
/* @ End
@*/

6. I can't think of anything else. Write it here for the moment.

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.