<a href="somepage.html">Click Me</a>
I wish to use javascript in the href tag:
<pre>
<a href="javascript:somefunction()">Click Me</a>
</pre>
i searched google, and i could only find examples that would pop up a new window (window.open()) with sompage.html on the new window. I wish for the current window to load sompage.html, not on a new window.
The reason is because I wish to generate random links to it.
Thanks.
[web] Javascript question
how to do this in javascript (sorry, uber noob here):
instead of:
The html still references to another page, i believe you have to catch the OnClick, OnOver events etc,
The best place for learning this stuff is
w3schools
Hope that helps,
Dave
The best place for learning this stuff is
w3schools
Hope that helps,
Dave
Use window.location
Example:
Example:
<html><body><script>function myfunction(){ window.location = 'http://www.gamedev.net';};</script><a href="javascript:myfunction()">Click Me</a></body></html>
I highly recommend you do not use javascript:
Most people don't know what javascript: does - javascript: runs the code after the :, then loads a document containing HTML which is its output (return value).<br><br>The only way of avoiding this is returning the value "void".<br><br>This is not a good idea, as on many browsers it creates race conditions, i.e. the browser is trying to load a blank page (with "" as its HTML), simultaneously it tries to load something else. One of them wins, but it's not necessarily always the same one.<br><br>Instead, use<br><br><pre><br><a href="mypage.html" onclick="return SomeClickFunc()">My funky link</a><br><script type="text/javascript"><br>function SomeClickFunc() {<br> window.location.href = "whatever.html";<br> return false; // suppress the original function of the click event.<br>}<br></script><br></pre><br><br>Which is much more sound - and will go to mypage.html if javascript is disabled or fails. Also use this for popups, with the appropriate URL in the href- even if JS is enabled, this gives you the added advantage that the link will be marked as "visited" appropriately so that CSS selectors like a:visited apply to it.<br><br>Mark
Most people don't know what javascript: does - javascript: runs the code after the :, then loads a document containing HTML which is its output (return value).<br><br>The only way of avoiding this is returning the value "void".<br><br>This is not a good idea, as on many browsers it creates race conditions, i.e. the browser is trying to load a blank page (with "" as its HTML), simultaneously it tries to load something else. One of them wins, but it's not necessarily always the same one.<br><br>Instead, use<br><br><pre><br><a href="mypage.html" onclick="return SomeClickFunc()">My funky link</a><br><script type="text/javascript"><br>function SomeClickFunc() {<br> window.location.href = "whatever.html";<br> return false; // suppress the original function of the click event.<br>}<br></script><br></pre><br><br>Which is much more sound - and will go to mypage.html if javascript is disabled or fails. Also use this for popups, with the appropriate URL in the href- even if JS is enabled, this gives you the added advantage that the link will be marked as "visited" appropriately so that CSS selectors like a:visited apply to it.<br><br>Mark
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement