Home >Web Front-end >JS Tutorial >Detailed explanation of javascript:void(0) usage in js_javascript skills

Detailed explanation of javascript:void(0) usage in js_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:45:451969browse

javascript:void(0) means no action is taken. Such as:

Copy code The code is as follows:

8ccb8f4959fb2db11df5c0d4ce14ed995db79b134e9f6b82c0b36e0489ee08ed

This means that this link does not perform a jump action and executes the onClick event.

I think code like this is common among those who have used ajax:

Copy code The code is as follows:

3aca98d048feecd64332d688d99c755ahere5db79b134e9f6b82c0b36e0489ee08ed

But what does void(0) here mean?

void is an operator in Javascript, which specifies that an expression is to be evaluated but does not return a value.

void operator usage format is as follows:

1. javascript:void (expression)
2. javascript:void expression

expression is a Javascript standard expression to be evaluated. The parentheses outside the expression are optional, but are a good practice to write. (Implementation version Navigator 3.0 )

You can specify a hyperlink using the void operator. The expression will be evaluated but nothing will be loaded into the current document.

The code below creates a hyperlink that will cause nothing to happen to the user later. When the user links, void(0) evaluates to 0, but has no effect on the Javascript.

Copy code The code is as follows:

92089eb7775f8b2da9a0f317f5f010abNothing will happen here63505a6f727f70c8bd4066f3066dcb9d

The following code creates a hyperlink that will cause the user to submit the form when clicking.

Copy code The code is as follows:

40cf5f64c15043244209fb8b7cc02494
Submit the form here63505a6f727f70c8bd4066f3066dcb9d

The difference between a href=# and a href=javascript:void(0) Several methods of linking

#Contains a location information

The default anchor is #top, which is the top of the web page

And javascript:void(0) only represents a dead link

This is why sometimes the page is very long and the browsing link is clearly #是

Jumped to the top of the page

This is not the case with javascript:void(0)

So it’s best to use void(0)

when calling a script

or7ce8961e120a815eeb46b3f04acc755f

414cf55ed2331ac9b67428e512c358bcetc.

Several ways to link

1.window.open(''url'')

2. Use custom functions

<script>
   function openWin(tag,obj)
   {
    obj.target="_blank";
    obj.href = "Web/Substation/Substation.aspx&#63;stationno="+tag;
    obj.click();
   }
  </script>

<a href="javascript:void(0)" onclick="openWin(3,this)">徐州</a>

window.location.href=""

Summary:

1 void(0) is used to perform certain processing, but does not refresh the page as a whole. However, if you need to refresh the page, you should be careful.

2 In fact, we can use 870cd383f150d4ad83c7c7790d241cd0, this sentence will perform a submit operation. So under what circumstances are void(0) used more often? Without refresh, of course it is Ajax. If you look at the Ajax web page, you will generally see a lot of void(0), :), so when using void(0) ), it’s best to think about whether this page needs to be refreshed as a whole

3 To put it bluntly, href="#" will refresh the page as a whole, but href="javascript:void(0)" will not

The above content introduces the meaning of javascript:void(0) in js. I hope it will be helpful to everyone.

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