I want to run a simple JavaScript function on a click without any redirection.
Is there any difference or benefit between putting the JavaScript call in the hr
In terms of javascript, one difference is that the this
keyword in the onclick
handler will refer to the DOM element whose onclick
attribute it is (in this case the <a>
element), whereas this
in the href
attribute will refer to the window
object.
In terms of presentation, if an href
attribute is absent from a link (i.e. <a onclick="[...]">
) then, by default, browsers will display the text
cursor (and not the often-desired pointer
cursor) since it is treating the <a>
as an anchor, and not a link.
In terms of behavior, when specifying an action by navigation via href
, the browser will typically support opening that href
in a separate window using either a shortcut or context menu. This is not possible when specifying an action only via onclick
.
However, if you're asking what is the best way to get dynamic action from the click of a DOM object, then attaching an event using javascript separate from the content of the document is the best way to go. You could do this in a number of ways. A common way is to use a javascript library like jQuery to bind an event:
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<a id="link" href="http://example.com/action">link text</a>
<script type="text/javascript">
$('a#link').click(function(){ /* ... action ... */ })
</script>
it worked for me using this line of code:
<a id="LinkTest" title="Any Title" href="#" onclick="Function(); return false; ">text</a>
I experienced that the javascript: hrefs did not work when the page was embedded in Outlook's webpage feature where a mail folder is set to instead show an url
First, having the url in href
is best because it allows users to copy links, open in another tab, etc.
In some cases (e.g. sites with frequent HTML changes) it is not practical to bind links every time there is an update.
Normal link:
<a href="https://www.google.com/">Google<a/>
And something like this for JS:
$("a").click(function (e) {
e.preventDefault();
var href = $(this).attr("href");
window.open(href);
return false;
});
The benefits of this method are clean separation of markup and behavior and doesn't have to repeat the function calls in every link.
If you don't want to bind every time, however, you can use onclick and pass in the element and event, e.g.:
<a href="https://www.google.com/" onclick="return Handler(this, event);">Google</a>
And this for JS:
function Handler(self, e) {
e.preventDefault();
var href = $(self).attr("href");
window.open(href);
return false;
}
The benefit to this method is that you can load in new links (e.g. via AJAX) whenever you want without having to worry about binding every time.
I use
Click <a nohref style="cursor:pointer;color:blue;text-decoration:underline"
onClick="alert('Hello World')">HERE</a>
A long way around but it gets the job done. use an A style to simplify then it becomes:
<style> A {cursor:pointer;color:blue;text-decoration:underline; } </style>
<a nohref onClick="alert('Hello World')">HERE</a>
In addition to all here, the href is shown on browser's status bar, and onclick not. I think it's not user friendly to show javascript code there.