Href = "JavaScript :;" or href = "javascript: void (0)", the difference?

I read about comparing href="#" and href="javascript:void(0)" , I also want to know about comparing

href="javascript:;" or href="javascript:void(0)" href="javascript:;" or href="javascript:void(0)" ,

ps: I use href="javascript:;" all the time instead of href="javascript:void(0)" , there were no problems.

+4
source share
4 answers

Using javascript: in hyperlinks in any form is bad. Your JavaScript design should be unobtrusive.

If you intend to attach event listeners to your anchors, you must use anchor # or even the actual URL, for example:

 <a href="/delete/post/123">delete</a> ... <script> jQuery(function($) { $('a').on('click', function(event) { event.preventDefault(); // do ajax stuff }); }); </script> 

Thus, older browsers (or those with JavaScript disabled) will follow the usual link to delete the item, while JavaScript-enabled browsers will do this via AJAX to prevent the page from updating.

If your link is really designed to do nothing, you should not use anchors at all; just use the <span> style to do this.

+2
source

The usual behavior for a javascript: link javascript: is that the body of the document is replaced with all the expressions the expression evaluates to, if it is not evaluated to undefined . There is no difference between your versions, they both evaluate to undefined , which in javascript: has no effect.

However, this is not the preferred way to create no-op links. Your uselessly forces the browser to parse and evaluate JavaScript. If you want to create a link that does nothing, you must set its href to "#" and the link click event binding function. An event object will be passed to the event handler; if you want to evaluate some code and then prevent link tracking (adding # to the current URL), you can use event.preventDefault() , event.stopPropagation() or simply return false from the event handler.

The best option for everyone is to keep a meaningful value in your href attribute, so if you bypass JavaScript (IE, the user opens the link in a new window), there is still some normal feedback. Then you must add extra rich actions on top of existing HTML-only functionality.

0
source

What you put in href is really not relevant. You should stop using the link in the first place by returning false from the click handler.

Example:

 <a href="fallback.html" onclick="doSomething();return false;"> 

In href you can put a link to a backup solution for users who have disabled Javascript, or a page telling them that they need Javascript to use the ones displayed on the page.

0
source

javascript: void (0) - easier to understand than javascript: some_code_to_execute. Of course, in your case javascript :; it is easy to read, one will not assume that the code is broken.

There are several reasons why handling lcicks with javascript is bad. I know that this is not a question, but related:

  • This approach is deprecated and is still supported by browser vendors, but not in wc3 standards.
  • The href = "javascript" link will not work for users without javascript. Better use href = "/somePage.com?id=123" onclick = "makeActionFor (777); return false">
  • Lack of separation between markup and code. In this case, enabling onclick in the previous example is also a poor way to bind a click handler.
0
source

Source: https://habr.com/ru/post/1502328/


All Articles