问题
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?
回答1:
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,
- 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:
- All of those redirects must be placed before any outputs to the client ok?
回答2:
I believe window.location.href = "newpage.html";
will work.
回答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.
回答4:
window.location.href = url;
It is ok for redirecting to the required url using javascript.
The simple example can be found in this url
来源:https://stackoverflow.com/questions/293506/javascript-sending-user-to-another-page