English 中文(简体)
Fixing a project with multiple Javascript frameworks
原标题:

I just started a new job, and my first task is to clean up the Javascript code for the site - the problem is that there are two JS libraries being used (jQuery and Prototype / Scriptaculous).

I m trying to figure out what s being used where, but it s pretty difficult, especially not being very familiar with the code.

Does anyone have any suggestions? I d probably prefer to drop Prototype / Scriptaculous if possible.

最佳回答

I would trash prototype from all includes on all pages, remove the noconflict in jquery & then keep firebug ready to show me all errors, and go over all the pages one by one, replacing functions with jquery equivalents, until things work right.

..pain in the butt method, but doubt you ll find less painful. Might be helpful: http://api.jquery.com/browser/

问题回答

I wouldn t suggest to drop a javascript library, as this would be a cross-sectional change. Very dangerous, and will cause spurious bugs now and in the future.

Chances are, that Prototype will be using the $ variable, and that jQuery will be running in noConflict mode, so it will be referenced by the jQuery variable.

It can get tricky, for instance:

 // here $ = Prototype

 jQuery(function($){
     // here $ = jQuery

 });

 // here $ = Prototype

And in separate files:

// here $ = Prototype
(function($){
    // here $ = jQuery;
})(jQuery);
// here $ = Prototype

Its just really important to pay attention to your context to see which library is being referenced. Additionally, its perfectly acceptable to ask questions here if you get stumped on how to do something in jQuery that was done in Prototype.

Getting rid of Prototype in particular could be wicked hard, especially if there were people working on the codebase who really "got" Prototype. I m speaking here about myself, having worked on a very large web application that in fact has exactly the mix you describe (although I think that by the time I left the Scriptaculous stuff was gone).

Because of the way Prototype works, there can be dependencies absolutely everywhere.

This is a bit of a daunting task. The suggestion to live and let live both frameworks is not a bad one, provided things are working. It is likely jQuery is in noConflict mode if they are really resident on the same page.

One tool that s very useful is to use the Web Developer Toolbar for Firefox and navigate to a page and do: Information -> View JavaScript -- this will give you a full listing of all the JavaScript script loaded with <script> tags, as well as inline JavaScript in <script> tags, though I think inline event handlers are not listed there.

But one thing you can do is simply unhook Prototype or JavaScript and see which ends you up with fewer errors. Then you re on your way to debugging what s missing.

It is easier to standardize on one library, but there s a case to be made for "if it ain t broke, don t fix it" too.

I ve taken some time to learn a bit about all frameworks, doing the same effects on identical HTML: ArtLung Rosetta: You can get a quick dive into the differences between syntaxes of Prototype and jQuery by comparing this and this.

Best of luck!





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签