SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    window.location to Refresh Page and go to hash(anchor)

    Ok I need to reload a page and go to an anchor.

    If I am currently on Page MyPage.html
    Code:
    window.location = 'MyPage.html';
    The current page "MyPage.html" will refresh (reload) but if I do

    Code:
    window.location = 'MyPage.html#main';
    The current page "MyPage.html" will no refresh (reload) will just look for the anchor named main.

    Any thoughts on how I could get it to both refresh and go to the anchor main?

    I did
    Code:
    window.location = 'MyPage.html';
    window.location.hash = 'main';
    But I goes doesn't work as expected. I could put a timer in there but I would like to know if there is another method for handling this.

    Thank you

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,445
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)

    to anchor

    The last I knew using "href" was optional, but things may have changed.
    Try "window.location.href ="

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mittineague

    Thanks for the suggestion I did try that and it didn't work either.

    Even though occasionally I a differnt page is request I tried to do reload() too. any other ideas?

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,445
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)

    to anchor

    If you make a direct request for 'MyPage.html#main' with your browser does it go to the 'main' tag OK?

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes. The Anchor tag is setup correctly. The problem I am having is that when I use

    window.location

    and add the hash it recongizes it as a local page therefor it does not refresh
    if I did

    Code:
    window.location = 'MyPage2.html#main';
    From MyPage.html

    then the code works as expected. The problem is the JS is saying hey you're already on MyPage.html lets just take you to the 'main' anchor. Instead of Refreshing the page then taking me to main anchor.
    The Best User Driven Poker Site.
    You can
    find Poker Games in your area and get the latest Poker Strategy.

  6. #6
    SitePoint Zealot
    Join Date
    Dec 2007
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to reload the page, I think you'll want to use the location.reload() method. I don't know if location.href will go back to the server (if it checks If-Modified-Since HTTP headers to see if the doc has changed on the server).

    I don't know if this will help at all, but it's just setting location.href to itself plus a named anchor.

    Code:
    <script type="text/javascript">
    function okGo() {
        var timer = setTimeout(nowJump, 3000);
        
        function nowJump() {
            location.href = location.href + "#wee";
        }
    }
    </script>
    
    
    <div style="width:500px; margin-left:auto; margin-right:auto;">
    <a href="javascript:void;" onclick="okGo()">Set location.href to itself + a named anchor</a> - after a 3 second delay.  This will jump to a named anchor after 3 seconds.
    <p>
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque ac sem nec metus tincidunt hendrerit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Duis nulla libero, scelerisque nec, ornare eu, euismod vestibulum, felis. Donec sit amet felis. Donec a sapien vitae nibh tempor blandit. Donec eu eros a libero convallis venenatis. Curabitur fermentum nonummy arcu. Fusce mattis leo vitae diam. Suspendisse purus quam, tincidunt id, egestas eget, dictum lobortis, tortor. In varius ipsum et metus. Donec ac ipsum. Morbi eros sapien, luctus quis, sagittis eget, dictum ac, nulla. Donec iaculis, sapien id lacinia sagittis, urna est bibendum est, a aliquam ligula sem vitae eros.
    <p>
    Fusce fringilla quam quis ante. Donec vel nunc. Vivamus sed enim. Morbi ac sem eu odio congue blandit. Mauris molestie, leo quis varius luctus, magna neque tempor pede, vel ultrices sapien tortor vitae sem. Nunc feugiat nulla id justo. In tellus mauris, bibendum ac, posuere et, varius in, lorem. Vivamus aliquet gravida dui. Proin vehicula purus. Nam facilisis sapien a risus. Nullam facilisis ipsum ut ante.
    <p>
    Maecenas leo mi, convallis eget, porttitor at, pulvinar at, eros. Suspendisse quis lacus. Ut ac eros. Praesent in elit. Duis condimentum. Ut ac libero. Praesent ultrices nulla vitae eros. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vestibulum varius neque sit amet nisl. Proin ac tellus lobortis velit facilisis gravida. Curabitur dapibus pede eget leo. Nullam sit amet lacus ut nisi pulvinar vulputate. Vestibulum lobortis ullamcorper dolor. Etiam vitae nibh. Maecenas viverra odio quis leo. Fusce erat libero, ultrices sit amet, pretium id, venenatis nec, turpis. Vivamus venenatis erat sed magna tempor lobortis.
    <p>
    Nulla facilisi. Duis sapien nibh, vulputate vel, elementum sit amet, porta ac, libero. Morbi scelerisque auctor libero. Suspendisse neque. Vestibulum placerat feugiat nisi. Donec eget nisl. Quisque mattis rhoncus eros. Suspendisse potenti. Etiam sagittis aliquet nibh. Pellentesque luctus, nunc quis facilisis adipiscing, ligula purus sollicitudin lacus, in ultrices metus libero eu quam. Aliquam ut dolor. Nullam non lorem. Cras interdum nisl at ligula. Nulla facilisi. Nulla nec tortor vel risus pretium fermentum. Morbi aliquam urna a diam. Quisque pretium aliquam massa. Nullam urna magna, commodo viverra, luctus ac, malesuada a, felis. Proin vitae purus ac erat scelerisque bibendum. Aliquam placerat.
    <p>
    Etiam sed urna eget mi dignissim pulvinar. Integer a libero facilisis nisi auctor auctor. In ultricies sem eu nisi. Vivamus sed lacus. Ut lorem tortor, convallis ut, malesuada rhoncus, nonummy ac, ipsum. Vivamus tempor, nisl nec eleifend ornare, nisi erat volutpat justo, in commodo nisi leo in turpis. Sed volutpat. Nulla sed diam sit amet lectus tincidunt tempus. Integer aliquam ligula vel elit. Sed augue.
    <p>
    <a name="wee"></a>
    <span style="background:red; font-weight:bold; color:#fff;">This is my named anchor</span> - 
    Curabitur id sem eu sem viverra ultrices. Cras convallis ante id sapien. Etiam quis pede. Vivamus venenatis diam in nisi viverra aliquam. Etiam venenatis. Quisque sodales, est quis vehicula pulvinar, nunc pede interdum massa, eu dignissim justo augue ut velit. Vestibulum vel sem a elit cursus elementum. Donec pretium eros suscipit quam vestibulum nonummy. Vestibulum ante ipsum primis in faucibus orci luctus.
    <p>
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque ac sem nec metus tincidunt hendrerit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Duis nulla libero, scelerisque nec, ornare eu, euismod vestibulum, felis. Donec sit amet felis. Donec a sapien vitae nibh tempor blandit. Donec eu eros a libero convallis venenatis. Curabitur fermentum nonummy arcu. Fusce mattis leo vitae diam. Suspendisse purus quam, tincidunt id, egestas eget, dictum lobortis, tortor. In varius ipsum et metus. Donec ac ipsum. Morbi eros sapien, luctus quis, sagittis eget, dictum ac, nulla. Donec iaculis, sapien id lacinia sagittis, urna est bibendum est, a aliquam ligula sem vitae eros.
    <p>
    Fusce fringilla quam quis ante. Donec vel nunc. Vivamus sed enim. Morbi ac sem eu odio congue blandit. Mauris molestie, leo quis varius luctus, magna neque tempor pede, vel ultrices sapien tortor vitae sem. Nunc feugiat nulla id justo. In tellus mauris, bibendum ac, posuere et, varius in, lorem. Vivamus aliquet gravida dui. Proin vehicula purus. Nam facilisis sapien a risus. Nullam facilisis ipsum ut ante.
    <p>
    Maecenas leo mi, convallis eget, porttitor at, pulvinar at, eros. Suspendisse quis lacus. Ut ac eros. Praesent in elit. Duis condimentum. Ut ac libero. Praesent ultrices nulla vitae eros. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vestibulum varius neque sit amet nisl. Proin ac tellus lobortis velit facilisis gravida. Curabitur dapibus pede eget leo. Nullam sit amet lacus ut nisi pulvinar vulputate. Vestibulum lobortis ullamcorper dolor. Etiam vitae nibh. Maecenas viverra odio quis leo. Fusce erat libero, ultrices sit amet, pretium id, venenatis nec, turpis. Vivamus venenatis erat sed magna tempor lobortis.
    </div>

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you magenta placenta, wanted to avoid simply setting a timeout variable. Maybe that's the only solution though.

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    If you set a querystring on the path with something that changes, like the time, it will be seen as a new location.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •