0

I was wondering how I might redirect to a page after a user enter information into a prompt and clicks OK, something like this:

var Input = prompt("Message:");
if (//Button clicked, and Input not empty\\) {
    //Sanitize input
    //Redirect to https://www.MyWeb.com/?Message=INPUT
}

Lots of people ask about things like alert() and confirm(), but I haven't found anything to do with prompt()

MaliciouZzHD
  • 49
  • 1
  • 12
  • Possible duplicate of [How to redirect to another webpage?](https://stackoverflow.com/questions/503093/how-to-redirect-to-another-webpage) – Spitzbueb Sep 20 '17 at 07:20
  • I know how to redirect, I just need to know how I would do this with both a `prompt()` and logic, most people do this with an `alert()` – MaliciouZzHD Sep 20 '17 at 07:21
  • Just like you did. The script stops and only continues once you entered something in the message box. – Spitzbueb Sep 20 '17 at 07:33

4 Answers4

1

This page will help you.

https://www.w3schools.com/jsref/met_win_prompt.asp

var Input = prompt("Message:");

if(Input.trim() != "" && Input == "your key")
{
    location.href = "/togo";
}
else
{
    alert("Fail!");
}

Is that what I understand?

good luck!

Java Mon
  • 51
  • 4
0

You can try like this

    var txt;
    if (confirm("Press a button!") == true) {
        txt = "You pressed OK!";
    } else {
        txt = "You pressed Cancel!";
    }
    document.getElementById("demo").innerHTML = txt;

With prompt:

   var msg = prompt("Custom Header:");
    if (msg.length > 0) { 
        console.log('Message Added ')
    } else {
        console.log('Its empty')
    }
MyTwoCents
  • 7,284
  • 3
  • 24
  • 52
0

Try like this .use with trim() it remove the unwanted space from the string

var str = prompt("Message:");
if(str == null){
  console.log('pressed cancel')
}
else if (str.trim()) { //validate is not empty with empty space also
  console.log('valid')
  //window.location.href = "url"
} else {
  console.log('empty')
}
prasanth
  • 22,145
  • 4
  • 29
  • 53
0

I thought up the following code:

var Text = prompt ( "Message" );
if (Text != "" && Text != null) {
    Text = Text.trim (  );
    location.href = "https://www." + Text + ".com/"
} else {
    document.write ( "Error!" );
}

It checks if the user either:

  1. Clicked cancel
  2. Clicked OK, but input nothing

The location.href and document.write were only for bug testing.

MaliciouZzHD
  • 49
  • 1
  • 12