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?
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?
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.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
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!![]()
Last edited by Wizard13335; 09-21-2005 at 09:41 PM.
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;">
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
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.
Last edited by Wizard13335; 09-23-2005 at 01:45 AM.
Sometimes the easiest way to kill a fly is with a sledgehammer, put this script in the head of the page:
Then all you need to do to reload the page is call:Code:<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>
doit()
This can be done in another script or wherever you like. To use it in a link on the page, do this:
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.Code:<a href="thisPage.htm" onclick="doit();return false;">Click Here to Reload the Page</a>
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
John, i dont think you understood that i was asking about the normal js link, not the one to refresh the page.
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:
Sorry I didn't read over everything before my 1st response. After doing so now, it looks like what your asking for is this:HTML Code:<a href="page.htm" onclick="window.location.href=this.href;return false">Link Text</a>
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.Code:onclick="window.location.href='page.htm'"
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
thanks John!
Last edited by Wizard13335; 09-25-2005 at 06:11 PM.
Bookmarks