Working with javascript opens up a whole set of possibilities for web developers. But at the same time, web developers have to deal with bunch of new issues when dealing with several browsers. The main reason behind this is because, each vendor has implemented the DOM in each browser differently. Some follows W3C standard while others don't. MS IE is the big bully in the block. IE usually doesn't respect W3C standards. So developers must put little extra effort from the very beginning of there development.
Always use a cross browser compatible javascript library for your DOM methods.
- YUI (http://developer.yahoo.com/yui/event/)
- JQuery (http://jquery.com/)Separate the view of the application (HTML) from its behavior (JavaScript).
Always be extra careful when dealing with dynamic event handlers.
Ex:(YUI is used in the following example and it outlines a practice example of the above three)
The html file (test.html)
<html>
<head>
<!-- Uncomment these lines if you want to keep YUI localy
<script type="text/javascript" src="yui/build/yahoo/yahoo-min.js"></script>
<script type="text/javascript" src="yui/build/event/event-min.js"></script>
<script type="text/javascript" src="yui/build/connection/connection-min.js"></script>
-->
<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/yahoo/yahoo-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/event/event-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/connection/connection-min.js"></script>
<!-- Source File -->
<script type="text/javascript" src="ext.js"></script>
<script type="text/javascript">
//when the DOM is ready execute the methord
YAHOO.util.Event.onDOMReady(init);
</script>
<title>Page Title</title>
</head>
<body>
<div id="messageOut" style="display:none;"></div>
<br /><br />
<form>
<input type="text" id="messageIn" />
<input type="button" id="messageButton" value="Test Me" />
</form>
</body>
</html>
ext.js
function init(){
YAHOO.util.Event.addListener("messageButton", "click", showMessage);
}
function showMessage(){
var messageButton = document.getElementById('messageButton');
var messageIn = document.getElementById('messageIn');
var messageOut = document.getElementById('messageOut');
messageOut.style.display = "";
messageOut.innerHTML = messageIn.value;
}
Note:To run the above example, you only need to save the two files “test.html” and “ext.js”. The YUI library files will be picked from the online sever.
Comments