• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is my javascript code throwing syntax error  RSS feed

 
Sucheta Shrivastava
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

I have written a javascript code in EditPlus++ and saved as javascript.js  ....as shown :




The code is throwing an error as -
line : 1
char :1

Syntax error

how to start it so that it shows up in html firefox
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66263
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is an HTML file, not a JavaScript file. The filetype should be .html, not .js.
 
Sucheta Shrivastava
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Bear

can you even figure out here y is the output not getting printed.  I have saved it as "javascript_cnsole.html" file

 
Rob Spoor
Sheriff
Posts: 21117
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
console.log prints to the browser console, but not to the browser window. You need document.write for that.
 
Nag Kakolla
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is few ways to print,

1. This will alert dialog with message 'Hello"

2.  This will write Hello to the DOM element so you will see on screen as HTML page gets rendered

3. if You want to see print of Original program, click F12 on Windows in Chrome browser and console.log is printed in Dev Console. in Other browsers, go to Dev console and you would see the print
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66263
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few things:

Nag Kakolla wrote:1. This will alert dialog with message 'Hello"

Be aware that alerts interrupt the flow of execution and aren't really a good way to debug. Rather, be sure that you have the Dev Tools open and use console.log() and its related console methods to print to the JavaScript console without blocking.

You can also set breakpoints in the debugger and actually stop execution at a certain point to look around at variable values.

2.  This will write Hello to the DOM element so you will see on screen as HTML page gets rendered

Also, be aware that document.write is a sledgehammer that's only evaluated while the page is initially begin constructed. It's OK for a novice to play with, but when you really want to add things to the DOM at run time, you'll need to learn the DOM API, or use a tool like jQuery (or others).

Read more about the Chrome Dev Tools here. Every major browser has an equivalent tool.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66263
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sucheta Shrivastava wrote:@ Bear
can you even figure out here y is the output not getting printed.  I have saved it as "javascript_cnsole.html" file


As pointed out, this will "print" to the JavaScript console in the Dev Tools. Be sure that the Dev Tools are open to see it.

I do not recommend changing this to alerts or to document.write. Those have different purposes than debugging. Use the console for debugging.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!