PDA

View Full Version : How to make my code cross-browser compatible??



shachi
09-20-2006, 06:26 PM
Hi everyone,
Can anyone tell me how do I make my code cross-browser compatible because every piece of code I make only works in firefox(even not every version of firefox) and not in other browsers. Any ideas how I can make my code work in *almost all* browsers??
Thanks for you patience reading this.

Twey
09-20-2006, 06:57 PM
What code?

shachi
09-21-2006, 10:26 AM
I meant everything I tried making with javascript(ajax chat, contact forms etc.)

blm126
09-21-2006, 10:56 AM
only use features that are suported in *almost all* browsers.

jscheuer1
09-21-2006, 10:58 AM
There really is no set way to achieve this. In fact, unless you can test in all browsers, there is no way to be certain that whatever you do, thinking that it will make it cross browser will work.

Most beginners like the idea of browser detection. This is virtually doomed to failure in some browsers because it is impossible to test for every browser out there and some will do their best to avoid detection.

I, and many more experienced javascript coders like to use feature detection. You've seen that, it is like:


if (document.getElementById)
var bob=document.getElementById('bober');

This is a good way to go about it as we are basically asking the browser if it does this and then asking it to do it if it does.

There is no real substitute for getting to know what sort of things this or that browser will do though. I don't think one's knowledge in this area could ever be complete but, the more you know, the better you are able to select the most universal sort of approach. It also helps you in being aware of what might cause a problem in one or more browsers so that you can provide code branches to handle these situations.

If you keep your code as simple as possible and use feature detection, providing branches for browsers with various capabilities to follow if they need to, things can usually work out.

shachi
09-21-2006, 02:59 PM
jscheuer1: unfortunately I neither have all the browsers except firefox nor do I know many objects in javascript to use object detection. Can you help me find out all the supported objects in browsers??

Twey
09-21-2006, 03:27 PM
I neither have all the browsers except firefoxGet some more. A single browser isn't nearly enough to thoroughly test a page.
Can you help me find out all the supported objects in browsers?QuirksMode (http://www.quirksmode.org/) helps. Alternatively, there's the good old-fashioned method: when one of your browsers complains that it can't find object X, it's pretty obvious that said browser doesn't support X. :)

jscheuer1
09-21-2006, 05:17 PM
A single browser isn't nearly enough to thoroughly test a page.QuirksMode (http://www.quirksmode.org/) helps. Alternatively, there's the good old-fashioned method: when one of your browsers complains that it can't find object X, it's pretty obvious that said browser doesn't support X. :)

I agree heartily with the above. I use Opera, FF, and IE and have plans to add others. I was using Konqueror but, the interface (cygwin) I had that allowed me to run it was too arcane for me to understand if I could and how to get the updates for Konq. I have plans to add other browsers and OS's. I'm careful with IE though, it really is a security nightmare, or at least a bad dream.

quirksmode.org is a great resource, somebody should give that guy a medal.

Also, when you get to a point where something isn't supported in a given browser, you can always Google the object and the browser, ex:

IE addEventListener

and it usually will find you the alternative, if any, used by that browser.

shachi
09-23-2006, 05:23 PM
Umm.. what if you can't install any browsers??

jscheuer1
09-23-2006, 05:28 PM
Umm.. what if you can't install any browsers??

Then you first need a computer.