...

View Full Version : Resolved Need help with hash removal detection in the url.



DJCMBear
12-17-2010, 04:30 PM
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.


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

Rowsdower!
12-17-2010, 05:28 PM
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.

DJCMBear
12-17-2010, 06:03 PM
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??

DJCMBear
12-17-2010, 06:56 PM
Right I have built this and it seems to run perfectly, thanks for the suggestion as well.



(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);
});



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum