views:

129

answers:

7

I'm pretty new to workign with Javascript.

In most languages you can run the code quickly locally on your machine. From what I've seen, in JS you generally only use it via the browser, and so I've been uploading my code an viewing its effects in the browser. This has proven very tiresome. Also, if I mak one error, it seems like my JS/JQuery will just do NOTHING, instead of giving me a useful error, message, which is making it painfully slow to code in.

IS there some way to run JS locally to see that it is working as I go? And then only upload it to the web when I'm mostly done? What ways are there for me to do this? What ways aer there for me to unit test the Javascript locally? Say I have some JAML that should render as <p>HI</p>, how do I run this locally in a unit test?

Thanks for the help, Alex

EDIT:

Thanks for all the great suggestions. I'll have to take a bit of time and go through them to see which ones best help me in my situation.

+4  A: 

You can use Firebug with Firefox to debug JS, and Google Chrome has a debugger built-in (use the Tools -> Developer Tools menu).

Groky
I agree, Firebug is the way to go.
fastcodejava
As far as I know you **can't do unit testing** with a stock version of Firebug which is what the OP is asking.
HoLyVieR
"IS there some way to run JS locally to see that it is working as I go?" - yes with firebug/chrome. He also asks about unit testing, I didn't answer this part of the question, nor did I claim to.
Groky
Firebug, my (and many others) favorite debugger
Mark
+1  A: 

You can run Javascript from the local file on your machine in your browser, so you can skip the uploading step.

Also, I'd recommend using Firefox/Firebug combo for developing Javascript as it will be very handy, especially for the part you mentioned about not seeing what's going wrong with your code.

Robert
+1  A: 

Even if you upload your javascript it gets downloaded back to you as soon as you visit the webpage that invoques it. Its run client side always. So stick to local and use firebug as the others have said. Google`s developer tool is quite nice too.

Iznogood
+4  A: 

Since you're using jQuery, I assume that you actually want to manipulate the various elements on your page. So depending on your specific development enviroment, uploading it each time is probably the way to go anyway. If you can set up a dev enviroment on your local machine (not always possible) then go with that.

As an actual answer to your question, I suggest using Chrome's developer tools, it doesn't just have the console, but an element inspector, and a resource tracker (resource tracker is invaluable when working with JSON and AJAX, since invalid json will fail silently)

As far as I know, the firebug plugin for firefox (dont use it myself) has a similar feature set, so if you're more comfortable with that go with it.

Just remember, as a developer, your development (and debuggin) enviroment is just as important as the code that you are writing.

EDIT: Noticed that you mentioned unit testing. There are several unit testing frameworks out there for JS including one that integrates with firebug called FireUnit. Do a quick google search to find more if you want.

Aatch
A: 

If you want to do unit testing with Javascript there are extension of Firebug that can help you with that. I haven't try any of them, so I can't really tell you which one are worth considering, but you can easily find them if you search for the keyword "Firebug unit testing" on Google.

What seems to be comming on top is FireUnit. You can find some information about how it works here.

HoLyVieR
+3  A: 

You don't need to upload the JS file to a server to test it. Just write an html and declare the js binding

<script
            src="js/yourJSFile.js"
            type="text/javascript"></script>

Edit the JS file in your favorite editor and then refresh the page to test it.

For unit testing the best option is Selenium. It allows you to record an interaction with the browser and then play it back.

Dani Cricco
A: 

Consider Spider Monkey, which is a javascript engine separate from a browser. If what you are developing does not involve rendering to a webpage or can be separated from the rendering code (good practice!), then this could be useful.