Javascript; Sending user to another page

送分小仙女□ 提交于 2019-12-12 08:21:47

问题


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,

  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?



回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!