0

I am sure this should be possible but when I look at google, all it tells me is how to get the filename and stuff, I don't want this.

I am looking for this because I use Weebly as my webhost and it only allows for .html files - when you click "Home" you are brought to /index.html and when you open the site there isn't a /index.html.

What would be a plus is if I can add (using an if statement of course) JS that redirects if the site's url == http://kingdomofmuqtasid.weebly.com/index.html and points the body to: http://kingdomofmuqtasid.weebly.com.

PS: can the code be placed inside the body?

2 Answers 2

2
window.location.href

is the full URL of the current page. See the MDN doc page for window.location for more info.

You could put this code in the <head> section or <body> section of your page to do the redirection.

<script>
if (window.location.href == "http://kingdomofmuqtasid.weebly.com/index.html") {
    window.location = "http://kingdomofmuqtasid.weebly.com/";
}
</script>

P.S. This code goes in whatever page is served up when the viewer goes to "http://kingdomofmuqtasid.weebly.com/index.html".

6
  • Thanks, but... how would I place that into an if statement so I can redirect between the 2 urls I listed?
    – user1522332
    Commented Jul 18, 2012 at 2:16
  • Sorry, I didnt see that! I just was notified on the other answer browsing the site so I jumped to the bottom. THANKS. I'll try it and mark it as an answer.
    – user1522332
    Commented Jul 18, 2012 at 2:20
  • Ok, I cannot accept the answer until 3 minutes are up but, thank you very much, the code works brilliant!
    – user1522332
    Commented Jul 18, 2012 at 2:22
  • Semi off-topic, but ideally you shouldn't be doing redirects on the client-side, you should be doing them server-side.
    – market
    Commented Jul 18, 2012 at 2:23
  • @market agreed. similar to going to the store, buying ice cream for your significant other, going back home, finding out its the wrong ice cream, etc etc
    – jbabey
    Commented Jul 18, 2012 at 2:26
0

You can use window.location to get these data. With the code above you can iterate over the window.location and get the data that you are needing.

var location = window.location;

for ( var i in location ) {
    console.log( i + " " + location[i] );
}
3
  • ok, thanks but... I need to somehow make an if statement that allows for redirects, I mean, how would I do that, I am a total newb to js, I have never used it.
    – user1522332
    Commented Jul 18, 2012 at 2:19
  • @rgbc, jfriend00 already said this in his answer. Take a look. Commented Jul 18, 2012 at 2:20
  • I just checked now, sorry, when I opened the page, I skimmed to the bottom. Thanks for your help.
    – user1522332
    Commented Jul 18, 2012 at 2:25