简体   繁体   中英

My javascript isn't recognized in chrome?

It's the first day for me to try javascript, and I use chrome developer tool now. The following example should print "My First JavaScript" to the website, but nothing is there. I put document.write("My First JavaScript") in the console, it just put "My First JavaScript" outside the <script> label, then prints the right result. I do google search to know that JavaScript is surrounded by a <script> and </script> tag, but it seems doesn't work in html. Please help me with this. Thanks!

<html>
 <body>
   <script>
     document.write("My First JavaScript");
   </script>
 </body>
</html>
<script type="text/javascript">
     document.write("My First JavaScript");
</script>

Java script tutorial

Try learning from scratch.. Am doing it now.. :P

I would take a step back and listen to some of the advice in the comments here. As a Javascript newbie myself, I found the most helpful tutorials were the interactive lessons at CodeAcademy.com

As for the actual code... what you describe is actually how it should work. Think of the console as being wrapped by those tags. Essentially, it's as if you put the command into a text file and included it in a script tag as you would for other JavaScript.

Document.write will take an HTML document and render it to your screen. In this case you're only sending it a single string (a bit of text) and the browser is building out the rest of that HTML that you see to make it a valid document.

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