开发者

Preserve state in jquery menu

开发者 https://www.devze.com 2023-04-12 20:46 出处:网络
I have jquery menu that on click shows specific div element. I wonder if i refr开发者_运维技巧esh page when for example div3 is active(visible), how can i make div3 active again after reload of the p

I have jquery menu that on click shows specific div element. I wonder if i refr开发者_运维技巧esh page when for example div3 is active(visible), how can i make div3 active again after reload of the page?

Thank you


How about using a cookie to track state?


I typically use #hash tag's in the URL and hash polling to achieve this.

Here is an example:

<a href="#showdiv">Click here to show div!</a>

var start_hash = window.location.hash;
var recent_hash = ""; 

process_hash(start_hash);  // Start the initial hash check
setInterval(poll_hash, 100);    // Initialize our hash polling interval

function poll_hash() {
 if (window.location.hash==recent_hash) { return; } // No change in URL hash
 recent_hash = window.location.hash;            // Set to check next time. 
 process_hash(recent_hash);             // Changed hash, lets process
}

// process_hash    
function process_hash(current_hash) {
 // Check for defaults, then set to #home.
 if(!current_hash || current_hash == '') { current_hash="#home"; }

 // Strip the # for the hash
 var hash_id = link_div.match(/[^#]+/g);                

 // conditions for multiple hash tags can go here. 
 if(hash_id == "#showdiv") {
  $("#somediv").show();
 }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号