0

I came up with the following script to perform actions on the page based on the #hashtag given in the url.

function checkhash(){

x = window.location.hash.replace("#", "");
$('#results').html(x); //Just to show the results, Delete in your code
//Put your functions here....

if (x == 'all'){
    $(".one").add(".two").add(".three").hide();
    $(".one").show();
                // and so on... just an example
}

window.setTimeout(checkhash, 1);
}

checkhash();

Is there a better method than above? I ask because in its current form it will continually run over and over again to check for a new tag (I assume because I cannot use fadeIn and fadeout methods as I would like to). My uses are for linking directly to /test.html#all to load specific content OR to link to #all on its page in the form of a button. Both are used for sorting purposes such as a button for all, blue, green, red and the page can be sorted by directly going to the link#hash or using the on-page button link of the #hash to show/hide specific content.

It works as is (without being able to use fade animations, but this is resource intensive due to it constantly running and I would like to avoid that if possible.

user756659
  • 3,372
  • 13
  • 55
  • 110

0 Answers0