16

I am trying to send a user to another page using a Javascript Function:

<input type="button" name="confirm" value="nextpage" onClick="message()">

And my JavaScript:

function message() {
    ConfirmStatus = confirm("Install a Virus?");

    if (ConfirmStatus == true) {
        //Send user to another page
    }
}

Does anyone know how to send a user to another specific page?

Amaury
  • 126
  • 10

6 Answers6

22

your code got messed up, but if I got it right you can use the following:

location.href = 'http://www.google.com';
or
location.href = 'myrelativepage.php';

Good luck!

But I must say to you,

  1. Javascript can be turned off, so your function won't work.

Other option is to do this by code:

PHP: header('Location: index.php');

C#: Response.Redirect("yourpage.aspx");

Java: response.sendRedirect("http://www.google.com");

Note:

  1. All of those redirects must be placed before any outputs to the client ok?
José Leal
  • 7,989
  • 9
  • 35
  • 54
  • Thank you. If Javascript can be turned off, is there a function in PhP that behaves like confirm() like in javascript? –  Nov 16 '08 at 05:03
  • Well, php is a passive form.. so you will need actually to send the response back to the server. Create one page to ask the user, sending the user response to another page, which will do the redirect (or not). – José Leal Nov 16 '08 at 05:37
8

I believe window.location.href = "newpage.html"; will work.

Community
  • 1
  • 1
titaniumdecoy
  • 18,900
  • 17
  • 96
  • 133
3

You can also use a meta refresh tag to redirect.

<meta http-equiv="refresh" content="2;url=http://other-domain.com">

Will redirect to the site http://other-domain.com after two seconds.

Matthew Schinckel
  • 35,041
  • 6
  • 86
  • 121
2

try this!

window.location.replace("http://www.link.com");
1

try this: window.location.href = "YOUR_RELATIVE_PATH_HERE" ex ./pages/aboutus"

Dylan
  • 2,161
  • 2
  • 27
  • 51
omid29
  • 11
  • 2
0

window.location.href = url;

It is ok for redirecting to the required url using javascript.

The simple example can be found in this url

Ramsharan
  • 2,054
  • 2
  • 22
  • 26