5

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.

Triad sou.
  • 2,969
  • 3
  • 23
  • 27
Jorge
  • 51
  • 1
  • 1
  • 2

4 Answers4

5

You could try this:

window.location.href = "/home";
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Fabrizio D'Ammassa
  • 4,729
  • 25
  • 32
  • it works but sends countless urls consecutively one after another... like if someone was refreshing the page countless times... – Jorge Sep 20 '11 at 08:18
  • Wierd, it looks like your js code is running on your home page. Can you double check? – Fabrizio D'Ammassa Sep 20 '11 at 08:22
  • the problem is with the "(location.host)"... If I replace it with "(location.href == 'http://THESITE.com/')" it works, but as I said it must be "universal"... – Jorge Sep 20 '11 at 08:34
  • The relative link "/home" is universal and it will build the url using the current protocol, hostname and port and adding '/home' as the pathname. You don't need to use location.host to adapt to different hosts. – Fabrizio D'Ammassa Sep 20 '11 at 08:39
2

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');
}
ephemer
  • 1,239
  • 8
  • 21
2

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?

Community
  • 1
  • 1
Xavi López
  • 27,550
  • 11
  • 97
  • 161
  • does not work too. sent countless urls consecutively one after another and end with an error from the server... – Jorge Sep 20 '11 at 08:05
  • So, the redirect is working, but the request is making the server fail? Does putting the same URL on your browser give the same results? – Xavi López Sep 20 '11 at 08:17
1

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.

  • it works but sends countless urls consecutively one after another... like if someone was refreshing the page countless times... – Jorge Sep 20 '11 at 08:18
  • I have used this method at several places, never had seen such consecutive requests. is that you doing it on page load such as every time the page loads the same js gets executes and again tries to set the url to the root ? – Vinay Chittora Sep 20 '11 at 08:39
  • Yes I must use it in a template that is universal to the entire website... is natural that it executes by itself.... because that I'm trying to use the "if" statement... – Jorge Sep 20 '11 at 09:00
  • can you please paste the code snippet.I am worried if it actually a problem then will try and find a solution. – Vinay Chittora Sep 20 '11 at 09:03