Home > Web Front-end > JS Tutorial > body text

What does Javascript:void(0) mean and how to use it in JavaScript?

黄舟
Release: 2017-11-09 16:08:33
Original
10633 people have browsed it

In our daily JavaScript development, we will encounter code like javascript:void(0). Many friends are confused when they see this and don’t know what it means. So this What does javascript:void(0) mean in JavaScript? Today we will take you to understand it together!

1. What does Javascript:void(0) mean and its meaning

The most critical thing in javascript:void(0) is the void key Word, void is a very important keyword in JavaScript. This operator specifies to evaluate an expression but does not return a value.

But what does void(0) here mean?

void operator usage format is as follows:

1. javascript:void (expression)
2. javascript:void expression
Copy after login

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 )

The syntax format is as follows:

<head>
<script type="text/javascript">
<!--void func()javascript:void func()
或者
void(func())javascript:void(func())//-->
</script>
</head>
Copy after login

1. Usage of Javascript:void(0)

javascript:void(0) means no action is taken. For example:

<a href="javascript:void(0);" onclick="alert(&#39;ok&#39;);"></a>
Copy after login

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

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

<a href="javascript:doTest2();void(0);">here</a>
Copy after login

Several ways to link

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

2. Use custom function

<script>
   function openWin(tag,obj)
   {
    obj.target="_blank";
    obj.href = "Web/Substation/Substation.aspx?stationno="+tag;
    obj.click();
   }
  </script>
 
<a href="javascript:void(0)" onclick="openWin(3,this)">徐州</a>
 
window.location.href=""
Copy after login

Summary:

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

2 In fact, we can use , this sentence will perform a submit operation. So under what circumstances is 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 ) before, it is 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, while href="javascript:void(0)" Otherwise

Related recommendations:

1.Let’s talk about javascript that is often used in static pages: ; and javascript:void(0)

2. Analysis of the difference between a tag href=# and href=javascript:void(0) in html

3.Use javascript:void(0) with caution, why is it not good to write like this

The above is the detailed content of What does Javascript:void(0) mean and how to use it in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!