search
HomeWeb Front-endJS TutorialSome skills you need to know in JS (DHTML)_Basic knowledge

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.

Statement
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
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!