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:
Code:
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.
Bookmarks