Results 1 to 9 of 9

Thread: JavaScipt Links

  1. #1
    Join Date
    Sep 2005
    Location
    Connecticut
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JavaScipt Links

    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?

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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!

  3. #3
    Join Date
    Sep 2005
    Location
    Connecticut
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post

    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.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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!

  5. #5
    Join Date
    Sep 2005
    Location
    Connecticut
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Sometimes the easiest way to kill a fly is with a sledgehammer, put this script in the head of the page:
    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>
    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:
    Code:
    <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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Sep 2005
    Location
    Connecticut
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    John, i dont think you understood that i was asking about the normal js link, not the one to refresh the page.

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:
    HTML Code:
    <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:
    Code:
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Sep 2005
    Location
    Connecticut
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thanks John!
    Last edited by Wizard13335; 09-25-2005 at 06:11 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •