Thursday, June 10, 2010

Tutorial: Special Concerns With CSS and JavaScript

If you want to edit existing CSS and JavaScript, I wish you the best. Here are some guidelines I have come up with for myself to try and minimize collateral damage. You see every css entry or JavaScript class might only be used in one place or it could be used in a thousand places. Sometimes if you change the padding or the position the tiniest bit it will throw something off someplace else.

My best suggestion is to build a class that will stand the test of time and then don't touch it. sometimes its even better to make a new specialized class rather than alter existing ones that could break countless pages. Also feel free to comment in the css if you need to to remember which class that is or which classes it goes with to match baselines.

If it is impossible to avoid editing your existing CSS or JavaScript classes, due to one reason or another(as is too frequently the case), I recommend doing a search over the app to see where it is used. If possible, test each and every one of the unique looking cases after you change whatever it is you need to change.

Some of the more benign (meaning they usually don't mess up where things are) things to change are:
background styles
font-family, font-size, other font-stuff
most white-space ones overflow:hidden

The moderately powerful (or dangerous) ones are:
border(border affects the width in Firefox)
white-space:nowrap(this can cause it to overflow

The very powerful ones are:
display:block,table-row, or none
float(necessary to line up divs or give spans widths in Firefox)
margin(in IE it can cascade)

Internet Explorer does not interpret everything in JavaScript the same way as other browsers. It has unique methods that the others do not have, and it lacks some things that the others DO have. Most of the time, you can fortunately put code in just two groups: IE and non IE.
In order to specify that you want code to execute in IE only or not there is an easy shortcut.
We just attempt to make a proprietary IE JavaScript object, also known as an ActiveXObject?
if(window.ActiveXObject)//IE code

}else{//Other browsers code


Now you might be wondering which things are IE only and which are Firefox. Most normal methods are supported in both. But here's a link to a nice list of the DOM methods
If you stick to the the DOM to do your work you should be ok in most cases but the following are some incompatible differences between browsers.

  1. myObject.getAttribute("className"); for IE, myObject.getAttribute("class"); for Firefox
  2. Getting the Size of the Viewport, or Browser Window: document.documentElement.clientWidth;(or Height) for IE, window.innerWidth; for Firefox
  3. Event codes! All event codes are differently handled and event is global automatically in IE and not in any other browsers. event.srcElement versus for Firefox to get the thing that started the event. In order to pass event along for firefox to use you must include it in the root method. I.e.

    Run like the Wind
  4. To get keyCodes from events use our allEve(event).key; method in Utils.js which works in all browsers
  5. After you do a var someNodes = selectNodes(yourXML or XSL) you must use someNodes.item(x); for IE and someNodes[x] for Firefox
  6. When you create a DOM element Firefox now requires you to declare its name-space: document.createElementNS("",'div'); but no other browser supports this.(yet) use document.createElement("div"); for most
  7. In IE you can easily make a node from a String like this openNode = 'false'; using our handy dandy createNewXmlNode(openNode) function. In Firefox you must build it from scratch with lots more code.
  8. instead of using .text .innerText, or .textcontent, instead use .firstChild.nodeValue which will work for both browsers
  9. appendchild works in both but it will not overwrite things so you must remove child first
  10. Using appendChild in Firefox on data that has been inserted via innerHTML will give you dead HTML that you cannot interact with. Use a floating/modal div to display form inputs instead if you can.
  11. font webdings only works in IE. The chars for arrows are 4 and 6. For Firefox you must use the actual HTML char values: '►' and '▼'

No comments:

Post a Comment