Attributes:
type: The language your script is in. Defaults to javascript since HTML5. In older browsers you were able to run alt languages, like VBScript in IE. In fact, it is supposed to actually point to the mime type of your script. The syntax is, for example: type=”text/javascript”
language: not used. Deprecated.
async: When set to true, makes the download of the script asynchronous, not holding the page hostage until it downloads. The default is false, so pages have to wait until this downloads before they go on. It is HTML5 supported. Moving scripts at the end of the page is a more compatible technique (with older browsers) to avoid scripts blocking page rendering, but don’t do it if you have document.write commands in your script. (async=”async”)
defer: waits until the DOM is ready before executing the script. It is HTML5 supported. It is overriden by async (defer=”defer”)
XHTML compatibility. If you use that as your doctype, you have to envelop your inline scripts in CDATA, to avoid special characters like & to cause problems:
<script type="text/javascript"> //<![CDATA[ // Your javascript here //]]> </script>
Example of dynamically adding scripts to a document:
var script = document.createElement("script");
script.setAttribute("src", url);
document.head.appendChild(script);