简体   繁体   中英

Return to index.html if refresh is hit on a hashtag link

我正在制作一个网站,当有人刷新时,我使用的整个jQuery脚本都会很奇怪-因此,我认为当有人刷新时,他们的URL从说www.website.com/index.html#test变为仅www.website.com/index.html ...如果可能,这怎么可能?

What you can do is check if the url contains hashes so:

var url=window.location;
if(url.indexOf('#')!=-1){
   window.location = 'index.html';
}

what the indexOf does is it checks if a string contains some substring and if it does then it returns the index of where that substring is and if it dosen't find it then it returns -1 so what we did there is we checked if it't not equal to -1 so that means the substring exists then we want to go to index.html

window.onbeforeunload = function(e) 
{
    window.location = "index.html#introduction";
};

This lets you redirect a refresh to another section but it didn't seem to work redirecting to index.html

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