a874 a874 - 5 months ago 11
Javascript Question

How to redirect to a html page from a javascript snippet?

I am new to javascript. I want that when a user enters the username at the time of signup and that name already exists in the database,then i will show a prompt that

Name already exists
. and you need to enter another username.

Now i want to have a button in the same prompt box clicking on which, the user will be redirected to the Signup page
signup.html
again.

I am unable to figure out how to do this.
What i have done is this---

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<script>
prompt("Username already exists\n please enter another username");
</script>
</body>
</html>

Answer

https://developer.mozilla.org/en-US/docs/Web/API/Window/location

you can put it in any js-script block you like. eg

<script>
  function redirectMe(url){
    window.location = url;
  }

  setTimeout(redirectMe('http://stackoverflow.com'}, 3000);
</script>

will redirect to stackoverflow after 3000ms (=3s)