最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Redirect base url to homepage - Stack Overflow

programmeradmin9浏览0评论

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 jquery 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.

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 jquery 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.

Share Improve this question edited Sep 21, 2011 at 0:34 Triad sou. 2,9713 gold badges24 silver badges28 bronze badges asked Sep 20, 2011 at 7:49 JorgeJorge 511 gold badge1 silver badge2 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 5

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');
}
发布评论

评论列表(0)

  1. 暂无评论