Prompt in javascript does not redirect

Asked

Viewed 109 times

0

I have the following script on a page:

<script>
    function fnPage(){
        var page = prompt("Para quel página deseja ir?");
        if (page != null) {
            location.href="http://" + page;
        }
    }
</script>

When I fill in with the O popup address it usually appears with space to fill in with an address, but the redirect does not work when I click OK. Does anyone know where the mistake is?

  • 1

    Try window.location.href

  • 1

    @Leonklaj many attributes of window can be accessed without calling window in fact.

3 answers

1

<script>
    function fnPage(){
        var page = prompt("Para qual página deseja ir?");
        if  (page!= null) {//so corrigir aqui nessa linha page dentro do parenteses
            location.href="http://" + page;
        }
    }
</script>
  • Thank you, but it was an error when posting here. In the code is the correct way: (page != null)

0

One more detail that I find interesting to add. If I add to the page:

<p id="test"> </p>

And change the script to:

function fnPage(){
    var page = prompt("Para qual página deseja ir?");
        if (page != null) {
            document.getElementById("test").innerHTML = page;
            location.href="http://"+ page;
        }
 }

The address appears on the page (in paragraph id="test"), but only flashes quickly, it doesn’t stay on the page. I don’t know if this helps.

  • It is because then Location.href changes the page.

  • I deleted the redirect line, and yet the address only flashes on the screen.

0

Correct the following passage: if page (!= null) changing to if (page!= null)

<!DOCTYPE html>
<html>
<head>
    <title>Teste</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <script>
        function fnPage()
        {
            var page = prompt("Para que página deseja ir?","http://");

            if (page!= null) 
            {
                if(page.indexOf("http://") == -1){ page = 'http://' + page; }
                location.href = page;
            }
        }
    </script>   

</head>
<body>
<input type="button" value="Mudar página" onclick="fnPage()">
</body>
</html>
  • This error I made when posting here. In the script is correct, and does not work. I have fixed the post.

  • But here the code worked. I tested this code that I pasted there below in Firefox, Chrome and IE11 (I needed to click a button to allow execution of scripts on the page), which browser you are using?

  • I’ve now added a code fragment that checks if http has been typed and if it hasn’t been, add it to the url.

  • I am using Opera. But I have other similar scripts on other q pages are working normally, with redirection from popups.

  • You tested that code of my answer at the Opera and it didn’t work?

  • 1

    I hadn’t noticed the change you made. Now it worked perfectly. Valew!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.