Page 25 - HTML5
P. 25
<a href="javascript:myFunction();">Run Code</a>
You can also achieve the same thing using the onclick attribute:
<a href="#" onclick="myFunction(); return false;">Run Code</a>
The return false; is necessary to prevent your page from scrolling to the top when the link to # is
clicked. Make sure to include all code you'd like to run before it, as returning will stop execution of
further code.
Also noteworthy, you can include an exclamation mark ! after the hashtag in order to prevent the
page from scrolling to the top. This works because any invalid slug will cause the link to not scroll
anywhere on the page, because it couldn't locate the element it references (an element with id="!"
). You could also just use any invalid slug (such as #scrollsNowhere) to achieve the same effect. In
this case, return false; is not required:
<a href="#!" onclick="myFunction();">Run Code</a>
Should you be using any of this?
The answer is almost certainly no. Running JavaScript inline with the element like this
is fairly bad practice. Consider using pure JavaScript solutions that look for the element
in the page and bind a function to it instead. Listening for an event
Also consider whether this element is really a button instead of a link. If so, you should
use <button>.
Link to a page on the same site
You can use a relative path to link to pages on the same website.
<a href="/example">Text Here</a>
The above example would go to the file example at the root directory (/) of the server.
If this link was on http://example.com, the following two links would bring the user to the same
location
<a href="/page">Text Here</a>
<a href="http://example.com/page">Text Here</a>
Both of the above would go to the page file at the root directory of example.com.
Link that runs email client
Basic usage
https://riptutorial.com/ 9

