[web] Javascript question

Started by
3 comments, last by _nomad_ 17 years, 9 months ago
how to do this in &#106avascript (sorry, uber noob here): instead of:

<a href="somepage.html">Click Me</a>
I wish to use &#106avascript in the href tag: <pre> &lt;a href="&#106avascript:somefunction()"&gt;Click Me&lt;/a&gt; </pre> i searched google, and i could &#111;nly find examples that would pop up a new window (window.open()) with sompage.html &#111;n the new window. I wish for the current window to load sompage.html, not &#111;n a new window. The reason is because I wish to generate random links to it. Thanks.
Advertisement
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
Use window.location

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 &#106avascript:

Most people don't know what &#106avascript: does - &#106avascript: runs the code after the :, then loads a document containing HTML which is its output (return value).<br><br>The &#111;nly way of avoiding this is returning the value "void".<br><br>This is not a good idea, as &#111;n 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. &#79;ne of them wins, but it's not necessarily always the same &#111;ne.<br><br>Instead, use<br><br><pre><br>&lt;a href="mypage.html" &#111;nclick="return SomeClickFunc()"&gt;My funky link&lt;/a&gt;<br>&lt;script type="text/&#106avascript"&gt;<br>function SomeClickFunc() {<br> window.location.href = "whatever.html";<br> return false; // suppress the original function of the click event.<br>}<br>&lt;/script&gt;<br></pre><br><br>Which is much more sound - and will go to mypage.html if &#106avascript 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
alright cool tips and help!

thanks guys!

This topic is closed to new replies.

Advertisement