简体   繁体   中英

Why is the browser showing the blank page?

I'm trying to run the code in a browser and after "Enter any String" command, it just shows the blank page.

HTML File:

<!DOCTYPE html>
<html>
<head lang = "en">
    <meta charset = "UTF-8">
    <title>PracticeCoding</title>
    <script language = "Javascript" src = "FirstReverse.js"></script>
</head>
<body>
    <script language = "Javascript" type = "text/javascript">
    var myString = prompt("Please enter any String");
        document.write("The reverse of the " + myString + " is " + reverseString(myString));

    </script>
</body>
</html>  

FirstReverse.js File:

function reverseString(myString){
    var reverseString = "";
    for(var i = myString.length; i > 0; i--){
        reverseString += myString[i];
    }
    return reverseString;
}

It is because of the document.write clause. When you execute it a new page is opened (because is intended to do so, to create a new document with your stream).

More info here: https://developer.mozilla.org/es/docs/Web/API/Document/write

Because your method reverseString is not defined when your code is being executed. Consider putting your code in the onLoad event or use jQuery domready event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM