javascript in <a href=...>

I think i've read on here before that you shouldnt really put javascript into <a href=http://www.webdeveloper.com/forum/archive/index.php/"...">, you should use onClick instead.<br />
<br />
This i've done, and I usually just put the bookmark symbol into the href like so:<br />
<br />
<a href=http://www.webdeveloper.com/forum/archive/index.php/"#" onclick="..."><br />
<br />
That's always been fine, just i've noticed that of course if i've scrolled down the page to click on this link, clicking on it takes me back up to the top of the page (as well as doing my onclick event).<br />
<br />
firstly am i right not to put javascript directly into the href, and secondly, if i am, what can i put into the href so that it still looks like a link but wont go jumping around the page?<!--content-->make sure your onclick returns false:<br />
onclick="dostuff(); return false;"<br />
or better yet<br />
onclick="return dostuff();"<br />
<script><br />
function dostuff()<br />
{ //do stuff<br />
return false;<br />
}<br />
</script><!--content-->am i right not to put javascript directly into the href<br />
Yes, you should put it in the event handler because otherwise pre-Javascript browsers (the _really_ old ones) would try to navigate to the code/function call as a URL.<br />
<br />
Adam<!--content-->I would say it is more of a accessibility issue. Having the code in onclick event allows you to provide the link to alternative content when the JS is disabled:<br />
<a href=http://www.webdeveloper.com/forum/archive/index.php/"backuppage.html" onclick="return dostuff()"><br />
When onclick function returns false the browser stops processing of the request to open the new URL, so in JS enabled browsers it does not matter what you put in href.<br />
<br />
You can take it one step further and branch the code depending on the browser:<br />
<a href=http://www.webdeveloper.com/forum/archive/index.php/"backuppage.html" onclick="return dostuff(this)"><br />
<script><br />
function dosfuff(link)<br />
{ if(document.getElementById) <br />
{ //do stuff;<br />
return false;<br />
}<br />
else<br />
{ if(confirm("Your browser sucks.\n OK to upgrade; CANCEL to view ugly backup page"))<br />
link.href=http://www.webdeveloper.com/forum/archive/index.php/"http://www.mozilla.org";<br />
return true;<br />
}<br />
}<!--content-->I have a really cool (but simple) Javascript that goes in the < A HREF=http://www.webdeveloper.com/forum/archive/index.php/""></A> tags. It does the same thing as your browser's back button:<br />
<br />
<A HREF=http://www.webdeveloper.com/forum/archive/index.php/"javascript:history.back(1)"><INPUT type="submit" value="Back"></A><br />
<br />
-PDAKING<!--content-->Originally posted by Dave Clark <br />
pdaking, you would do better to leave the link out (with modern browsers, anyway) and just use a .gif, that looks like a button, with the word "Back" on it:<br />
Another option would be to only write the link in if the users have javascript as seen below:<br />
<br />
<script language="javascript" type="text/javascript"><br />
document.write ('<a href=http://www.webdeveloper.com/forum/archive/index.php/"javascript:history.back(1)">Back</a>');<br />
</script><!--content-->...round and round in circles we go! The point of this thread was that you shouldnt be putting the javascript in the href, so put it in the onclick like it says above!<br />
<br />
Also, one problem of creating a gif button is of course that the buttons are os dependednt now - pre win xp they're the square ones, but anyone who's turned on the fancy thingamejiggs in xp will get nice curvy ones.<br />
<br />
Can you do a button (form item) and put that java into an onclick for that button, and not bother with the a href at all? I would ahve thought that you could but havent tested it...<!--content-->Originally posted by jpmoriarty <br />
...round and round in circles we go! The point of this thread was that you shouldnt be putting the javascript in the href, so put it in the onclick like it says above!Actually all I was trying to point out was that since the back button won't work without javascript anyway, you might as well only right it in, if they have javascript. I always use onclick. I just took pdaking's code and didn't even look at it. :( But yes, you are right, you should not use javascript in the href. Had I looked closer at what I was posting, this is what I would have done... :rolleyes: <br />
<br />
<script language="javascript" type="text/javascript"> <br />
document.write ('<a href=http://www.webdeveloper.com/forum/archive/index.php/"#" onClick="history.back(1); return false;">Back</a>'); <br />
</script><!--content-->
 
Back
Top