What is the correct href value for a javascript tag tag?

I usually have to bind a JavaScript function to a binding event. This is easy to use with jquery or the onclick built-in attribute.

But, my problem is that I never know what the best way to keep href empty .

For instance:

  • <a href="javascript:void(0)"> - It seems that there is too much code to just be empty.
  • <a href=#> - If I do not want to go to another page, I must return false in a JavaScript call
  • <a href> - This option violates the cursor and hover, and the browser does not display it as a link.
  • <a> - idem

What is the maximum href value for empty anchors? I'm not interested in maintaining functionality without JavaScript

+4
source share
6 answers

The correct one is to use the empty a element href attribute and bind the click event in Javascript.

For an unobtrusive design, you should have an href attribute with an appropriate link (so those with Javascript can still use the site) and remove the attribute in Javascript by attaching a click event.

If you just use the a element as an object to bind the click event, use a div or span instead.

+3
source

Just don't use element A. You can also make a DIV clickable or any other element.

Or you can also just leave the href attribute, for example.

 <a onclick="myFunction();">dasd</a> 

If you also want to see it as a link, put this in CSS:

 a { text-decoration: underline; color: blue; }​ 
+1
source

I personally strongly believe in using JavaScript for extension , not replacement. With that being said, I leave anchors indicating a “safe” drop in the action that I actually perform using javascript. Simply put:

 <a href="/users/create" class="user-create"></a> 

Then add (and return false) if javascript was able to successfully load and bind to the element, otherwise provide the user with the opportunity to complete the task if they do not have javascript (either blocked through a plugin, or simply not loaded).

+1
source

I think,

 <a href="javascript:;"></a> 

or

 <a href="javascript:void;"></a> 

is the best way to indicate an empty anchor.

and

 <a href="#someId"></a> 

will move the page to a dom element that has id = "someId"

0
source

it all depends. if you click on the anchor to open the panel on the page, I will gladly use option 2, but only if I insert this anchor with javascript.

this means that when javascript is disabled, the anchor is not displayed and the panel should be visible.

If the link goes somewhere, then you need the actual address of the link, for example, brad christy and odid.

0
source

<a href=#>ABC</a>

=> when you click on the link, it will set the URL in the address bar, which makes the document flicker or reset the scroll position

to avoid the problem above, <a href=# onclick="return false" >abc</a> can be used and bind an event handler using jQuery, as usual, or you can execute any onclick function that returns a false value.

0
source

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


All Articles