JavaScript development - bookmarking lines of code? - javascript

I constantly find myself rewriting the same lines of code in the Firebug console to test my application. The application uses UI so much that I don't think unit tests are the solution here? How could I quickly run lines of JavaScript code without typing them again and again?

You can use bookmarklets to do exactly what you're talking about: Running JavaScript in the context of the current window from a bookmark. Bookmarklets need to be fairly short and can be a pain to write (because they're javascript URLs, and so need to be URL-encoded), but you can use a bookmarklet to bootstrap an external script (written normally) into the page and then just call functions in that script, which minimises how much actual bookmarklet code you have to write. More about doing that (including an example) in this answer here on SO.
But in terms of testing: Presumably the UI is underpinned by logic code, it's worth having unit tests for that. There are also unit testing frameworks specifically for web applications that you can use for testing UI features; see this SO question/answer for more. The one you seem to hear about most right now is Selenium.
A couple of others have mentioned GreaseMonkey, and in FF that's certainly an option. The nice thing about bookmarklets is that they work in a wide range of browsers.

To run JavaScript code you have to type it. Or save it somewhere.

Related

Disoriented when writing JavaScript - Architectural Integration - Grockage

I'm a reasonably experience programmer (10+ years), and have only hacked JavaScript. I don't feel comfortable writing more than a few lines of it, but I'm seeing it more and more unavoidable to learn properly.
I get the same feeling writing JavaScript as I first did writing Unix shell scripts. I didn't fully understand the environment, where things are going, and so forth. It took a few well-timed suggestions to sort it all out.
I have a hard time with Js tools. I'm okay with almost any text editor, but is there anything other than alert('foo') and Firefox error console to see what the heck is going on?
Ps. I have bought books. Yes, I can write code snippits, but nothing sufficiently explained the whole paradigm. It's all code snippits, with no larger picture of how to integrate it architecturally with server side scripts, and not have to duplicate everything on the server side.
Maybe that's the answer. . write some on the server side?
edit:
tl;dr AMA OO PHP programmer. how do I quickly and completely grok javascript/jquery?
This article might be helpful to you: Organizing A jQuery Application. It dissects a non-trivial jQuery application and explains how to architect a whole JavaScript application (as opposed to code snippets). You can download the code there, and sing along.
Learn to use the console tab of Firebug, including breakpoints. It's a simple and powerful way to see what's actually going on in your scripts.
Firebug also gives you a JS command line to work with, which is a great way to experiment with short snippets and see what they do.
http://getfirebug.com/javascript

How do you know if a JavaScript library you are using will break your code after an upgrade?

So, you are using a bunch of javascript libraries in a website. Your javascript code calls the several APIs, but every once in a while after an upgrade, one of the API changes, and your code breaks, without you knowing it.
How do you prevent this from happening?
I'm mostly interested in javascript, but any answer regarding dynamically typed languages would be valuable.
I don't think there's much you can do. You always run a risk when updating any piece of software. The best advice is to:
Read and understand documentation about upgrading
Upgrade in your test environment
TEST
Roll out live when you are happy there are no regressions
You should consider building unit tests using tools such as JsUnit and Selenium. As long as your code passes the tests, you're good to go. If some tests fail, you would quickly identify what needs to be fixed.
As an example of a suite of Selenium tests, you can check the Google Maps API Tests, which you can download and run locally in your browser.
Well there are two options:
Don't upgrade
Retest everything after you upgrade.
There is no way to guarantee that an upgrade won't break something. Even if you have something that could check the underlying API and make sure it still all lines up, you can't be certain that the underlying functionality is the same.

Writing a non-GUI bot using Mozilla Framework

I'm looking for a way to write a non-GUI bot using Mozilla Framework. The bot should be able to work like normal browser (automatically download relevant JS files, make XMLHTTPRequests, run JS operations, modify DOM), except no GUI will be needed.
I wonder if it is possbile to build XULRunner without X, GTK/KDE (without any GUI dependencies), as I will run the bot on FreeBSD server 6.4.
It may sound a bit weird but I need a bot with capacity to operate like browser, runs JS, modifies DOM, submit forms running on non-GUI environments.
I've looked into other browsers such as Lynx, Links, Hulahop, Chrome V8 engine, WebKit JavascriptCore but yet to find desirable output.
It's a part of school project, thesis. We will use to observe price change of budget airlines and after one year long data collection, we need to deduce pricing strategy and customer behavior. It is a serious Final Year Project.
Any hint or help is greatly appreciated! Thank you in advance!
Regards.
You should be able to make progress with selenium. It's a record/test/play tool but its core is manipulating the DOM.
Update from Grundlefleck's comment: As for launching the actual tests there is selenium remote-control, which allows you to write your tests in Java, Ruby, plain HTML and other possible drivers.
Yes, it is possible (but it might very well require LOTS of code changes).
No, I do not know any of the details.
I would not recommend this approach for your purposes. From your comment, it sounds like you are trying to scrape webpages. If you really need to use JavaScript, you can use a stand-alone JavaScript-engine (Mozilla's is available here). Otherwise, I would use Beautiful Soup with Python or Twill. You might also want to read this question.

Are GWT wrappers on top of javascript libraries discouraged?

I'm in a process of selecting an API for building a GWT application. The answer to the following questions will help me choose among a set of libraries.
Does a third-party code rewritten in
GWT run faster than a code using a
wrapped JavaScript library?
Will code using a wrapped library
have the same performance as a pure
GWT code if the underlying
JavaScript framework is well written
and tuned?
While JavaScript libraries get a lot of programming eyeballs and attention, GWT has the advantage of being able to doing some hideously not-human-readable things to the generated JavaScript code per browser for the sake of performance.
In theory, anything the GWT compiler does, the JavaScript writers should be able to do. But in practice the JS library writers have to maintain their code. Look at the jQuery code. It's obviously not optimized per browser. With some effort, I could take jQuery and target it for Safari only, saving a lot of code and speeding up what remains.
It's an ongoing battle. The JavaScript libraries compete against each other, getting faster all the time. GWT gets better and better, and has the advantage of being able to write ugly unmaintainable JavaScript per browser.
For any given task, you'll have to test to see where the arms race currently places us, and it'll likely vary between browsers.
In some cases you don't have another option. You can not rewrite everything when moving to GWT.
In a first step you could just wrap your existing code in a wrapper and if it turns out to be a performance bottleneck you can still move the code to Java/GWT
The code optimisation in GWT will certainly be better than what the majority of JS developpers can write. And when the Browsers change, it is just a matter of modifying the GWT optimizer and your code will be better tuned for the latest advances in Js technology.
Depends on how well the code is
written.
I would think so.
Generally look at the community around a 3rd party library before using it unless it is open-source (so you can fix bugs) and specifically look for posts concerning bugs - how quick do the maintainers respond to items. How long is a release cycle, etc.

Is there a decent browser-based javascript self-editor?

Is there a decent browser-based javascript self-editor?
It is obvious one can make a quick js editor with a page containing a form textarea, some buttons and callbacks. I'm wondering if someone has taken that as a beginning and ran with it.
The javascript to be edited could be defined in a global string or it could be a served .js
The ideal editor would show a pretty version inside a browser window and provide some kind of development environment for editing the script.
It is understood that user-written scripts would only exist inside the browser and could not be saved without some additional server-side functionality. The ideal package would discuss and explore this.... but I'd settle for anything that just lets the user make their own simple changes to 100-200 line scripts.
I'm not sure exactly what you're asking for, but does jsbin satisfy your needs?
The hard part of what it sounds like you want to do is going to be parsing the javascript so that you can do intelligent things with it. The CodeMirror library can help you develop something to put on a page.
UPDATE:
Etherpad isn't around anymore but Ace is really nice. It's got a long history and is the engine used by the Cloud9 IDE
There is
http://sourceforge.net/projects/codepress
http://www.cdolivet.com/index.php?page=editArea&sess=4b049cee4477f0820c82231b09e80a91
http://marijn.haverbeke.nl/codemirror/
Do also check out Bespin.
There are also integrated solutions which, in addition to browser-based online editing of JavaScript code, do other things for you, like running the JS code in the browser or hosting it for you.
If you're interested in those, try Erbix.com.
coderun

Categories

Resources