Jump to content

Which is the proper way to inline JS in HREF?


Videogamer555

Recommended Posts

I've seen it done a number of different ways for using JS links where the HREF section is followed by an ONCLICK section.

<a href="#" or using javascript:void but neglecting the proper ending semicolon<a href="javascript:void" or with a space after the ":"<a href="javascript: void" or with a "()" after the "void"<a href="javascript:void()" or with a (0) after the "void"<a href="javascript:void(0)" or some combination<a href="javascript: void()"<a href="javascript: void(0)" or one of the above entries, but this time with the proper ending semicolon symbol<a href="javascript:void;"<a href="javascript: void;"<a href="javascript:void();"<a href="javascript:void(0);"<a href="javascript: void();"<a href="javascript: void(0);"

Please help me to figure out which I should use for the HREF part of a link, when using javascript inline with that link. Thanks in advance. Also I've noticed, in the ONCLICK section the JS command isn't always followed by a semicolon. Isn't it always REQUIRED to use a semicolon after a JS command? So doesn't that mean that the semicolon is required, even for "inline" JS? So many different web pages use different standards, that I bet most of them are officially "out of spec", but the browser (at least firefox) seems to "know" the proper way to handle all of these variations, rather than rejecting all of the wrong ones improper and generating an error, such that only the proper syntax of inline JS would produce the desired result. So I find it difficult to learn the correct JS standard for inline JS by merely viewing other websites' implementations of it. So yeah, if any of you know the proper way to write inline JS in the HREF and ONCLICK sections, please help me. Thanks. with

Edited by Videogamer555
Link to comment
Share on other sites

Adding javascript: void(0); is a very outdated technique and is not necessary.. Since void() is a method, the parenthesis are required. I don't know what the parameter is for so whether or not you put a 0 is beyond my knowledge, though I could look at the mozilla developer network to find out.. semi-colons are optional in Javascript, as line breaks indicate the separation between statements. It's a good practise to always put semi-colons because if the whitespace happens to be stripped out your code will become useless. These days, you should always have a proper URL in the href attribute, you can prevent the link from working in the onclick event handler.

Link to comment
Share on other sites

The "correct" answer would say to not put Javascript inline at all, but attach Javascript events in the body's load handler or another place. In that case the href would point to the page you're going to anyway, and the click handler would cancel the default navigation and do something else (open in a new window, for instance). This allows the link to still function if Javascript is disabled. If the link isn't associated with another URL then an anchor isn't preferred, it would be better to use a button or even a div with an onclick handler. Besides that, since window.void is a function then you use parentheses after the function name to call it. It doesn't matter if you pass a parameter to it. Javascript does not require that statements end with a semicolon. My mistake, void is an operator which always evaluates to undefined, not a function. Parentheses are not required. https://developer.mo.../Operators/void

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...