简体   繁体   中英

JavaScript code gives blank page in Safari

I recently dipped my toe into JavaScript and very quickly ran into some problems. I am trying to run a simple "Hello World" program that is not working. Whenever I open the html file in Safari, I get only a blank page. JavaScript IS enabled in Safari preferences. The file is saved with the .html extension. Here is the code I have written in Sublime Text 2:

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title> JS Test Page </title>
</head>

    <script type=“text/javascript”>
          document.write(“hello world”); 
    </script>

<body>

</body>
</html>

If it helps, the path file in the URL is: file:///Users/ME/Desktop/testJS.html

You need to put your script tag inside the head or body tag.

There was also a problem with the double quotes. They were unicode characters. Possibly you copied them from a website using a weird font or something instead of typing them and that was messing things up.

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title> JS Test Page </title>

    <script type="text/javascript">
          document.write("hello world"); 
    </script>
</head>


<body>

</body>
</html>

You need to put your script in head tag.

Both document.write method should output text into an unready (open) document.

Read http://javascript.info/tutorial/document-write for further info.

Did you copy and paste this code?

<script type=“text/javascript”>
    document.write(“hello world”); 
</script>

You error is only because your double quotes, try this code below:

<script type="text/javascript">
   document.write("hello world"); 
</script>

and put your script tag inside the body or head tag.

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