views:

267

answers:

7

Is it in general very hard to port JavaScript code to another browser platform?

+7  A: 

Not if you use a library like jQuery or Prototype.

But if you don't, then it can be a real bitch. See quirksmode for the everything cross-browser.

Here are some common problems:

  • eventing
  • AJAX
  • id vs. name
  • iframes
  • ... i could keep going but ppk (quirksmode) says it best
geowa4
+1 for mentioning quirksmode. I don't like jQuery/Prototype but the general idea (abstraction for browser-independence) is a good one.
Jason S
Yeah, I'm not going to list every library. That would go on forever... ExtJS, YUI, Dojo, Glow, SproutCore, ...
geowa4
+12  A: 

You don't need to "port" javascript, since it runs in every major (graphical) browser.

Cross-browser issues are a massive pain for JS developers, but the pain is being eased by libraries such as jQuery which smooth out a lot of the key differences.

nickf
well, in a stretched sense, you do need to port. what you wrote to work in one browser will likely not run in another, unless the function is very basic.
geowa4
I meant porting from the platform Firefox to the platform Internet Explorer. It is fully equivalent to porting from one OS to another.
Dimitri C.
No it's not fully equivalent at all. There are some inconsistencies in how some browsers have implemented the standards, but most things are the same and can be worked around. You'd never have to maintain two separate codebases or anything like that...
nickf
A: 

Yes

Crimson
A: 

The JavaScript shuold run fine for you ... unfortunately, tho', different browsers have different names for the same thing, etc. Try jQuery at jquery.com.

dcpking
+1  A: 

No, cross browser issues are extremely minor with regard to JavaScript. The most common differences are the IE expects use of the className method to change a class attribute instead of the getAttribute function. IE also requires a special way of entering the style block in the head, but otherwise cross browser issues are extremely rare in all vaguely modern browsers.

minor? seriously? have you written much plain js that supported multiple browsers and multiple versions of each? making a single xhr object would take something like 15 lines.
geowa4
Yes, seriously. I have written huge applications in JavaScript without the need of frameworks. The largest exception to the rule is the damned xmlHttpRequest object. If you are not using AJAX it is unlikely a programmer will ever see a cross browser difference. The JavaScript application I have completed so far is 75k after being minified.
modal windows, .children[], .childNodes[], .appendData(), .innerHTML, .textContent/.innerText, ... <-because those never come up.
geowa4
I have never had a problem with innerHTML and I use it exhaustively. I wouldn't suggest anybody use DOM methods if it could be helped, because they are extremely slow. Reading or writing HTML form an array and then using a single innerHTML method to write output from a joined array is 3.5 times faster on average. Honestly, there probably are many cryptic differences, but I write massive programs and these differences almost never arise.
+1  A: 

With libraries like jQuery, the compatibility issues are pretty much history. However, they won't help the least with CSS and DOM problems, which can cause horrific headaches with IE6.

af
CSS box model problems are not JavaScript problems.
they're becoming history as IE approaches standards compliance...
geowa4
They might become history when IE6 becomes history, and as Microsoft has extended it's life support to last until at least 2012, it won't be soon.
af
+3  A: 

If you're coding pretty simple JavaScript or using established libraries such as JQuery or Mootools then you'll probably not suffer any issues in regards to browser compatibility even in IE6 as these libraries have been thoroughly designed to comply to legacy browsers.

Steve