Prompt in javascript does not redirect

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 populate with the address of The popup appears normally with space to fill with an address, but the redirect does not work when I click OK. Does anyone know where the error is?

    
asked by anonymous 10.02.2017 / 13:31

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>
    
10.02.2017 / 13:45
0

Correct the following excerpt: 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>
    
10.02.2017 / 13:43
0

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

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

And I 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 the paragraph id="test"), but only blinks quickly, it does not stay on the page. I do not know if that helps.

    
10.02.2017 / 14:04