Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it bad practice to use links with the javascript: "protocol"?

In the 1990s, there was a fashion to put Javascript code directly into <a> href attributes, like this:

<a href="javascript:alert('Hello world!')">Press me!</a> 

And then suddenly I stopped to see it. They were all replaced by things like:

<a href="#" onclick="alert('Hello world!')">Press me!</a> 

For a link whose sole purpose is to trigger Javascript code, and has no real href target, why is it encouraged to use the onclick property instead of the href property?

like image 801
zneak Avatar asked Mar 19 '10 18:03

zneak


People also ask

What is JavaScript protocol?

The iterable protocol allows JavaScript objects to define or customize their iteration behavior, such as what values are looped over in a for...of construct. Some built-in types are built-in iterables with a default iteration behavior, such as Array or Map , while other types (such as Object ) are not.

Which href value should I use for JavaScript links or JavaScript Void 0?

Fast-forward to 2013: javascript:void(0) violates Content Security Policy on CSP-enabled HTTPS pages. One option would be then to use href='#' and event.

What is a JavaScript link?

In JavaScript, link() is a string method that is used to create the HTML <a> element with a hyperlink. Because the link() method is a method of the String object, it must be invoked through a particular instance of the String class.

Should I use JavaScript void 0?

Combining javascript: and void(0) Sometimes, you do not want a link to navigate to another page or reload a page. Using javascript: , you can run code that does not change the current page. This, used with void(0) means, do nothing - don't reload, don't navigate, do not run any code.


2 Answers

The execution context is different, to see this, try these links instead:

<a href="javascript:alert(this.tagName)">Press me!</a> <!-- result: undefined --> <a href="#" onclick="alert(this.tagName)">Press me!</a> <!-- result: A --> 

javascript: is executed in the global context, not as a method of the element, which is usually want you want. In most cases you're doing something with or in relation to the element you acted on, better to execute it in that context.

Also, it's just much cleaner, though I wouldn't use in-line script at all. Check out any framework for handling these things in a much cleaner way. Example in jQuery:

$('a').click(function() { alert(this.tagName); }); 
like image 109
Nick Craver Avatar answered Sep 23 '22 20:09

Nick Craver


Actually, both methods are considered obsolete. Developers are instead encouraged to separate all JavaScript in an external JS file in order to separate logic and code from genuine markup

  • http://www.alistapart.com/articles/behavioralseparation
  • http://en.wikipedia.org/wiki/Unobtrusive_JavaScript

The reason for this is that it creates code that is easier to maintain and debug, and it also promotes web standards and accessibility. Think of it like this: Looking at your example, what if you had hundreds of links like that on a page and needed to change out the alert behavior for some other function using external JS references, you'd only need to change a single event binding in one JS file as opposed to copying and pasting a bunch of code over and over again or doing a find-and-replace.

like image 25
Levi Hackwith Avatar answered Sep 19 '22 20:09

Levi Hackwith