简体   繁体   中英

Open page as if Nav Element clicked?

I have a single page site that hides and shows elements depending on which nav element has been clicked. Can anyone advise how to open this page as if one of the elements has been clicked already? I'm trying to put a link somewhere else that will open the page and show the 'gigs' element immediately.

The site is www.brookebentham.co.uk

use hashes

http://www.site.com/#tab/firstTab

then on page load or onhashchange event , look at the hash and do something based on it

$(document).ready(hashChanged);
window.onhashchange = hashChanged;

function hashChanged() {
   var hash = window.location.hash;
   var tab = hash.replace("tab/","");
   $("#"+tab).show();
}

or you can use the history.pushState (if supported) and just check the path

//assume something like as url http://www.site.com/tab/firstTab

//history has changed (ie on using pushSate)
window.onpopstate = function(){
   //path will be "/tab/firstTab"
   var path = window.location.pathname;
   var tab = path.replace("/tab/","");
   $("#"+tab).show();
};

$("#firstTab").click(function(e){
   e.preventDefault();
   history.pushState({},"Title (not really used","/tab/firstTab");
});

To fully use the pushState you would need the backend to do url rewriting, like apache's mod_rewrite to point all urls to your one page.

Try this

$(window).on("load", function(){
    $("#gig").show();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM