Redirect base url to homepage
I need to find the base url and redirect it to the 'home' page. Also I need to use relative expressions so that I don't have to edit the js file and it could be used in diferent websites.
I'm using jqu开发者_JAVA百科ery and so far I have this:
if (location.host) {
location.replace((location.host) + '/home');
}
But is not working... I am new to js and I need help.
You could try this:
window.location.href = "/home";
Maybe you want to do window.location.replace(location.host + '/home');
?
Also, take a look at this question: How can I make a redirect page in jQuery?
Well, you can just do this :
window.top.location.href = "/home";
It will automatically takes the domain and just adds the specified string and sets it. It works in all the browsers but its good to show an message like
The page will automatically redirect you to http://some.com/url/index.html, if it doesn't then click here.
I realise this is a very old question but maybe someone will stumble across it like I just did.
I'm pretty sure this is the answer the OP wanted:
if (window.location.pathname === '/') {
window.location.replace('/home');
}
精彩评论