0

how do i redirect to a welcome page from an index page using javascript?
The site is offline and on a development machine and not in a server www directory like (wamp, lamp, apache etc) .
I would like to do it without using PHP, python etc cause I already know how it is done in php using header(location ... ).

The directory structure


site
|
|--img
|--css
|--index.html
|--welcome.html
|--error.html

I have already tried window.location, window.location.href etc. Inside the script of index.html.

if (true){
    self.location("welcome.html");
}
else{
    window.location.href = "error.html";
} 
edi9999
  • 19,701
  • 13
  • 88
  • 127
Vyas Senthil
  • 173
  • 2
  • 12
  • 1
    Why use "if (true) {...} else {...}" ??? true will always be true so you don't need the else or the if – frenchie Jul 08 '14 at 13:33
  • Any errors in the console? – j08691 Jul 08 '14 at 13:33
  • window.location is the way to go, did you get any script errors? – Andrew Walters Jul 08 '14 at 13:34
  • self.location does not exist. That's it – edi9999 Jul 08 '14 at 13:34
  • as @frenchie said, your conditional logic is not correct for this purpose. try this if(window.location.href == "file:///C:/path-to-site/index.html") { window.location = "file:///C:/path-to-site/welcome.html" } – dangdis Jul 08 '14 at 13:40
  • A quick search brought up this http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-in-jquery-javascript – Francis Dolan Jul 08 '14 at 13:42
  • @edi9999 `self.location` does exist, `self === window`. – jshthornton Jul 08 '14 at 13:42
  • Right, didn't even know this – edi9999 Jul 08 '14 at 13:48
  • Hi Guys. Thanks for all your advice. But I was able to do it myself using window.location method. I found that the method was not the problem because I had tried it already previously. The problem was that the redirection happened but since the function "checkLogin" was called by the onsubmit function of html element form, it kept coming back to the same login page. I fixed it by returning false at the end of the checkLogin() script. But Thanks a ton for all your input. – Vyas Senthil Jul 09 '14 at 05:00

4 Answers4

1

Though Window.location is a read-only Location object, you can also assign a DOMString to it. This means that you can work with window.location as if it were a string in most cases: window.location = 'http://www.example.com' is a synonym of window.location.href = 'http://www.example.com'.

Mozilla Docs -- Window.location

It is not a function but you can assign a string to it.

Just write:

window.location="error.html";

self.location="error.html"; is fine too

edi9999
  • 19,701
  • 13
  • 88
  • 127
0

``This is very simple to do a page redirect using JavaScript at client side. To redirect your site visitors to a new page, you just need to add a line in your head section as follows:

<head>
<script type="text/javascript">
<!--
window.location="http://www.newlocation.com";
//-->
</script>
</head>
edi9999
  • 19,701
  • 13
  • 88
  • 127
0

You could also use a meta-refresh redirect: <meta http-equiv="refresh" content="0;URL='welcome.html" />

Or just do window.location="welcome.html" instead of self.location("welcome.html")

craig
  • 285
  • 2
  • 10
0

Thanks for all your advice.

But I was able to do it myself using window.location method. I found that the method was not the problem because I had tried it already previously.

The problem was that the redirection happened but since the function "checkLogin" was called by the onsubmit function of html element form, it kept coming back to the same login page. I fixed it by returning false at the end of the checkLogin() script. But Thanks a ton for all your input.

Vyas Senthil
  • 173
  • 2
  • 12