views:

1478

answers:

7

hopefully the question doesn't sound stupid, but there are lots of examples out there of achieving certain things in javascript/dom using jQuery. Using jQuery is not always an option (or even a want) which can make understanding the examples of javascript solutions written in jQuery hard.

Is there an easy way to convert jQuery code to regular javascript? I guess without having to access or understand the jQuery source code...

edit (future readers): pretend there is a logical reason why jQuery isn't available!

+5  A: 

The easiest way is to just learn how to do DOM traversing and manipulation with the plain DOM api (you would probably call this: normal JavaScript).

This can however be a pain for some things. (which is why libraries where invented in the first place).

Googling for "javascript DOM traversing/manipulation" should give plenty of helpful (and less helpful) resources.

The articles on this website are pretty good: http://www.htmlgoodies.com/primers/jsp/

And as Nosredna points out in the comments: be sure to test in all browsers, because now jQuery won't be handling the inconsistency's for you.

Pim Jager
And you have to be sure to test against various versions of each browser.
Nosredna
Indeed. Half the point of jQuery is that it sorts out all the horribe cross-browser JS compatibility issues for you.
Noldorin
that is true, and why i use jquery whenever i can. this particular application is an intranet only environment with a company wide standard workstation setup...politics prevent jquery being used in the app!
davidsleeps
How does the choice of desktop workstation influence jQuery use?
DDaviesBrackett
I'm a bit how surprised at how dominant jQuery is on StackOverflow. Do y'all think that's representative of where programmers are going with JavaScript? Looks like jQuery is just plain mauling Prototype, mooTools, Dojo, ExtJS, etc. And those other libraries are all pretty damned useful.
Nosredna
"How does the choice of desktop workstation influence jQuery use?" I don't think it does, except that jQuery is in Microsoft's tools, and is going to be supported by Script# in a release that's supposed to come along later this year.
Nosredna
If jquery is being pushed out by politics, is there some other similar framework you can push in its place? Anything you can do to abstract away some of that drudgery is a good thing.
Chris Farmer
Sorry, two seperate points: 1. Adding a javascript library into this application would require paperwork beyond its worth. it's crazyness. That's the politics game 2. The intranet environment has a standard workstation which means all applications on the intranet have a configuration that is to be assumed...it's all a bit backwards...So Internet Explorer 6, windows xp...installing other software is restricted (this restriction doesn't apply to developer machines).
davidsleeps
a further point...knowing jQuery (insert preferred library here) clearly is no substitute for understanding the dom and javascript...so learning hat is on.
davidsleeps
I sympathize with the politics of using an open-source library for some organizations. In the end the time you're spending rewriting plainly (and likely buggily) into plain DOM traversal and JS is wasted money for the company. You might make the case that large companies, including very conservative ones, use jQuery: http://docs.jquery.com/Sites_Using_jQuery: Oracle, Google, Amazon, Dell, Bank of America, Intuit, Salesforce. These are hardly hippie communist companies. These are very real companies whose legal departments have probably done their homework and accepted jQuery...
artlung
@davidsleeps: It's good to learn the underlying model, even if you end up using a library later.
Nosredna
... you have to pick your battles, but if you have complex enough web apps or widgets on your intranet using a library, jQuery or otherwise is going to speed your development and save the company money.
artlung
@artlung it makes sense to me and would save time/money for the company...but the people who make these decisions aren't programmers and if the benefit isn't obvious to the user then it can be hard to justify...changing things internal to an application can be tricky...
davidsleeps
@davidsleeps - jQuery is sanctioned by Microsoft...It is included in Visual Studio 2008, which technically makes it a "Microsoft" tool. Would that make your employer feel better?
Robert Harvey
Even if you could get Sizzle, Peppy, Selector.js, or Sly in as a selector engine, you'd be way ahead of the game. How about inline? Or at least look at them all and write your own selector engine. Check out Selector.js--it's TINY!
Nosredna
@davidsleeps "the people who make these decisions aren't programmers and if the benefit isn't obvious to the user then it can be hard to justify...changing things internal to an application can be tricky" It takes political skill to do that. Be an advocate for your work and professionalism! Robert Harvey's point is excellent: MSFT has embraced jQuery. There is no more corporate IT choice than Microsoft, except maybe IBM or Oracle. I have a hard time working in an environment where my technical judgment's questioned by nontechnical folk. I wish you well regardless - sounds like hard situation.
artlung
On the other hand, if your target is ONLY ie6, at least you don't have to worry about testing in the other browsers.
Nosredna
It's a great point about Microsoft adopting/endorsing jQuery as it will be a big win and make my case a lot easier. I've had that blog post from ScottGu announcing it saved away for a little while now! Developing for only ie6 actually is a horrible thing, because you feel that anything you write just won't work if it were to be used in another browser...
davidsleeps
What is the difference to your company between jquery and a whole bunch of javascript that you wrote to do the same thing? Its not like you are installing anything, coding in a new language or adding ANY dependancies. This is honestly the stupidest thing I've heard, allowing javascript but not jQuery. jQuery IS JAVASCRIPT!
micmcg
Just use jquery and don't tell anyone. If they're not technical enough to understand why you want to use it, they're not technical enough to discover you used it. You could even just rename the file to mylib.js for further cover.
Breton
@micmcg - you mustn't have your source code audited...i understand that jQuery is javascript, but if something doesn't work in the jQuery code, jQuery will release new updates which then means that any applications using it also need updating which requires change controls and approvals...further more, you are relying on code written by a third party for which you have no support agreement...
davidsleeps
Support agreements, I've noticed, aren't worth the paper they're written on. But maybe I just haven't worked in the biz long enough to see them pay off.
Breton
+1  A: 

Jeremy Keith's book "DOM Scripting" is a great intro to working with Javascript and the DOM. I highly recommend it, whether you want to use jQuery or not. It's good to know what's going on beneath.

dylanfm
A: 

There are several good books available. I like ppk on JavaScript. Here's Chapter 8 on the DOM.

Nosredna
A: 

If you want to learn javascript, watch these Doug Crockford videos. They are very good.

Theory of the DOM

bobobobo
+3  A: 

Is there an easy way to convert jQuery code to regular javascript?

No, especially if:

understanding the examples of javascript solutions written in jQuery [is] hard.

JQuery and all the frameworks tend to make understanding the code easier. If that's difficult to understand, then vanilla javascript will be torture :)

rpflo
A: 

I can see a reason, unrelated to the original post, to automatically compile jQuery code into standard JavaScript:

16k -- or whatever the gzipped, minified jQuery library is -- might be too much for your website that is intended for a mobile browser. The w3c is recommending that all HTTP requests for mobile websites should be a maximum of 20k.

w3c specs for mobile

So I enjoy coding in my nice, terse, chained jQuery. But now I need to optimize for mobile. Should I really go back and do the difficult, tedious work of rewriting all the helper functions I used in the jQuery library? Or is there some kind of convenient app that will help me recompile?

That would be very sweet. Sadly, I don't think such a thing exists.

A: 

do you solver this problem now?

i have this problem now,same as yours,so i want to know how did you handle this convertion? could you tell me something about this?

my email:[email protected]

thanks!

NJU_xpen
I didn't vote this down, but I'll make the comment; this is not an answer.
Homer