Saturday, June 9, 2007

A more complete guidance for JavaScript

Here I try to collection a complete guidance for JavaScript while I am working on that. First of all, JavaScript is not and unrelated to the Java programming language! Search a little bit, you'll find a lots of articles on that.

For beginners, I would like direct you to W3schools' JavaScript Tutorial. W3schools is a place where you can find good tutorials for most of web technologies.

For advanced programmers, you may want to look at the Core JavaScript Reference and the ECMAScript Specification, which is the standard for JavaScript (Netscape) and JScript (Miscrosoft).

I also collection some interesting knowledge about JavaScript.

  • The for and event attributes of the script tag can associate the code with the event. The following example defines JScript code and associates it as the onmouseover event handler for the img element that has the identifier MyImage. But this can only work with IE 6, not Firefox 2.0 on my computer.
    <script for="MyImage" event="onmouseover" 
    type="text/javascript">
        // Carry out some work, e.g.
        alert("hey!")
    </script>
    ...
    <img id="MyImage" src="sample.gif">
  • You can also set the background of an element in class "bar" by executing a JavaScript.
    <style>.bar{background-image:url("javascript:alert(’JavaScript’)");}</style>
    
  • The example above indicate that JavaScript can also be used as a protocol as ftp and http. For example, you can enter the following url in the address bar of your browser to see what happend.
    javascript:alert("Hello World")
    
    The script will be executed and an alerting box will pop up.
  • String.fromCharCode() write Unicode characters. You can get Unicode values (be prepared to download a 33M file.
  • When you put a block (say, with id="id1") inside a [select] block, you cannot use document.getElementById("id1") to get the object of the block. Solution: move the block out of [select] block.
  • Get text of a span block, use innerHTML property.

No comments: