Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts

    Need help with hash removal detection in the url.

    Hey I am in need of some help with a hash removal detection code, currently I have everything I need to do what I want but when I click the back button and the hash is removed from the url I need to have a script run to then change the page content.

    My design looked sort of like this.
    Code:
    function detect() {
      var url = window.location;
      if(url.indexOf('#') === -1) {
        // run the code
      }
    }
    But that ran the code every time there wasn't any hash in the url so if a user went to lets say /index.php the page content would keep being re-generated as there isn't any hash, so my question is how can I make it so it only runs only if there has been a hash in the url and then has been removed so like if you went to /index.php and clicked a link that took you to /index.php#tags and then the page content was changed with ajax, then the user clicked the browsers back button to go back to /index.php, the page would still have the tags content on it so that's when I need the function to run to change it back again.

    If anyone can help with my problem please reply.

    Thank you
    - DJCMBear
    Last edited by DJCMBear; 12-17-2010 at 07:07 PM.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    You could track the page status with a cookie value that records the "current" page. Then, onload and oncheck for a hash, if the cookie's stored value for the current page is one with a hash, you run the reload script and then set the cookie's value back to the standard page url without a hash.

    That way if there is no cookie or if the value does not have a hash you won't reload into eternity.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #3
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Thanks for the reply, Basicly I am building a fastSwitcher like facebook where you click an internal link it adds it to a hash so if your on /index.php and click a link to /news.php the URL will change to /index.php#!/news.php and the news page will be displayed, then if you click a link to a news item you could get /index.php#!/news/1234/this_is_a_news_item.php which then the news item is displayed. But this is where the problem starts as the page content changes when I go back to /index.php#!/news.php but not when I then click back to go to /index.php.

    I was thinking of having a system the triggers a global value once the hash key is in the URL and then only have it do the checker interval then as the global value is false once page starts until the hash key is there then only run the content changer once.

    Would that work??
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #4
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Right I have built this and it seems to run perfectly, thanks for the suggestion as well.

    Code:
      (function($){
        $.fastSwitch = {
          Active: false,
          Once: true,
          Checker: function() {
            if(!$.fastSwitch.Active && (window.location+'').indexOf('#!/')!==-1) {
              $.fastSwitch.Active = true;
              $.fastSwitch.Once   = true;
            } else if($.fastSwitch.Active) {
              if((window.location+'').indexOf('#!/')===-1 && $.fastSwitch.Once) {
                alert('triggered');
                $.fastSwitch.Once   = false;
                $.fastSwitch.Active = false;
              }
            }
          }
        };
      })(window);
    
      window.onload = (function(){
        setInterval("fastSwitch.Checker()",1000);
      });
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P


  •  

    Posting Permissions

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