PDA

View Full Version : JavaScipt Links



Wizard13335
09-15-2005, 06:31 AM
Hi everyone. I know how to create javascript links for a popup window (<a href="#" onclick="window.open ('nameofpage')), but how would you create a normal link that opens in the same page using javascript, or is it not possible?

Twey
09-15-2005, 08:50 AM
You can do it: <a href="page.htm" onclick="window.location.href = this.href;">
But... why?
P.S. If you have a Javascript link, always put "return false;" on the end, and instead of using # or javascript:void(0), set the href to the page so non-JS browsers can get the page as well.

Wizard13335
09-21-2005, 09:28 PM
The reason i needed to do that is because i needed to set an onclick option for something, and i didnt want to make a new window. So, what part of that example code would need to be edited for use? (im a javascript newb) Also, how would you create a link to refresh a page? Thanks alot for your help! :)

Twey
09-22-2005, 06:45 AM
To add an onclick, you'd edit the onclick attribute (onclick="code"). To refresh the page, use:
<a href="thispage.htm" onclick="window.location.href = window.location.href;">

Wizard13335
09-23-2005, 01:16 AM
No, what part of the earlier code, the normal js link (window.location.href = this.href) should be edited to suit my page? Where would the location of the page go, ect.

jscheuer1
09-23-2005, 02:26 AM
Sometimes the easiest way to kill a fly is with a sledgehammer, put this script in the head of the page:

<script type="text/javascript">
<!--
function doit(){
if (window.location.reload)
window.location.reload( true );
else if (window.location.replace)
window.location.replace(unescape(location.href))
else
window.location.href=unescape(location.href)
}
//-->
</script>Then all you need to do to reload the page is call:

doit()

This can be done in another script or wherever you like. To use it in a link on the page, do this:

<a href="thisPage.htm" onclick="doit();return false;">Click Here to Reload the Page</a>Substitute the actual name of the page this link is on for thisPage.htm and that way even non javascript enabled browsers will do a reload on click.

Wizard13335
09-25-2005, 12:03 AM
John, i dont think you understood that i was asking about the normal js link, not the one to refresh the page.

jscheuer1
09-25-2005, 02:35 AM
There isn't really a normal javascript link. It depends upon what you want to have happen and, not only in the javascript enabled browser, but also in the non javascript enabled browser. The closest I can think of for one would be:

<a href="page.htm" onclick="window.location.href=this.href;return false">Link Text</a>Sorry I didn't read over everything before my 1st response. After doing so now, it looks like what your asking for is this:

onclick="window.location.href='page.htm'"That can be put as an onclick event to almost any html element. The red part is where you put the actual path/filename.ext that you want the link to go to. Pay close attention to the use of quotes, both the single ones around the destination and the double ones around the event itself.

Wizard13335
09-25-2005, 04:34 AM
thanks John!