Why JS operation of the HREF is javascript:void (0); _ Basic knowledge

Source: Internet
Author: User

One, I am a little white

The second half more impetuous, have a good meditation study, farewell to small white. Decisively bought a few books:

Among them, the first to see the "JavaScript mode"-many people think very shi a book. ...... Evaluation Impressions, etc. 500 words omitted ... When I see the end of chapter two, once again saw the "JS to avoid using void" view, so, to hook up as a small white I have long been a problem: "Since JS is void not recommended, then why Sina Weibo, Taobao flow of the home page JS Operation href is javascript:void(0); it?" ”

Screenshot as proof!

Why? Why? All the lines? All the lines?

Second, the small white doubt

Hundred tore not to ride elder sister, hundred tore not to ride elder sister!

Whenever void I see, I think of n years ago, just contact the program C language that will nightmare, the university has only hung the branch of the C language, every time reminds me of that dry female lecturer. Like me this kind of vanilla good student, before the university net bar has not been to the good schoolmate, also not first with the program what to use, the beginning of the crackling variables ah, the sentence ah, xxx, every day against the black background of strange things knock A, B, C, that feeling is like a child was in the river at the bottom of the choking drink, foggy, In the water, in the soil. I think of a story I saw when I was a child, a young man in the countryside who was fortunate enough to go to England as an exchange student, living in ordinary people's home (naturally very enthusiastic), but not a few days, the lad was driven away-the reason is: ya urine does not lift the toilet cover, but there are lady, too disrespectful! In fact, the first time this young man saw the toilet is in this family, he did not know what the toilet cover is used for, when the urine to lift up!

Is the habit or the norm, I am puzzled! The so-called flow javascript:void(0) of the atmosphere I guess is the ya some C, c + + so-called program background to bring, can be traced back to 10 ...

I just opened the QQ mailbox, so I looked at its href value, for javascript:; :

It can be proved that it void(0) is really superfluous and useless, so why do so many websites use this? Every time you have to knock more than 7 extra characters, this is to make what, Murphy performance is based on the amount of HTML code to calculate!

Hundred tore not to ride elder sister, hundred tore not to ride elder sister!

Then, I have further questions: The last semicolon in JavaScript can be the default, so why use it instead javascript:; javascript: ?

Is the habit or the norm, I am puzzled!

Coder with code cleanliness, nothing more to write a semicolon, the Holy Spirit of the World Cup tainted, how can endure it? This is not a lot of statements, do not write a semicolon may appear unexpected situation!

Like me this lazy hand cramp, write a character will be difficult to be on the ground rolling people, it is really think not to come ah!

Perhaps because I am a little white, the World of God I do not understand.

Three, I am a small white

I have been using it for href="javascript:" many years and have been using it with trepidation, because everyone else is href="javascript:void(0);" , is it because href="javascript:" of any potential problems? For example, affect performance? Or is it impossible to identify the toilet cover?

Ask for advice, ask for spit groove!

Let's make a simple summary:

Refer to the following link for a description of void:

Http://www.jb51.net/w3school/js/pro_js_operators_unary.htm

For the above question, borrow the code from the link:

<a onclick= "FN ()" >does not appear as a link, because there ' s no href</a> <a ' href= 
(0) "O Nclick= ' fn () ' >fn is called</a> <a href= ' javascript:undefined ' onclick= ' 
fn () ' >FN is called</a > 

The above three are all equivalent.

In addition to attach a netizen to javascript:; and javascript:void (0);

You see, JavaScript scripts can often see code like this:
〈a href= "Javascript:dotest2 (); void (0);" 〉here〈/a〉
But what does void (0) mean here?
In JavaScript, Void is an operator that specifies that you want to evaluate an expression but not return a value.

void operator usage format is as follows:
1. javascript:void (expression)
2. Javascript:void expression

Expression is an expression of the JavaScript standard to compute. The parentheses outside the expression are optional, but writing is a good habit.

You can use the void operator to specify a hyperlink. The expression is evaluated but does not load any content in the current document.

The following code creates a hyperlink that will not happen when the user clicks on it. When the user clicks on the link, void (0) evaluates to 0, but there is no effect on JavaScript.

The difference between void and #

# contains a location information the default anchor point is #top, which is the top of the page, and javascript:void (0) only represents a dead link.

This is why sometimes the page is very long browsing links obviously is # but it beats to the top of the page, and javascript:void (0) is not so it is best to invoke a script with void (0).

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.