<script> Tag Examples
Examples of the script
tag in HTML 5
<script type="text/javascript" src="external-script-file.js"/>
<script type="text/javascript"> <!-- window.onload=alert("Hello JavaScript World!") // --> </script>
The HTML comments in this example do not comment out the JavaScript code. They will only hide the code from browsers without JavaScript support or with scripts disabled. In some cases, browsers give the user an option to disable scripts. This HTML code pattern was designed to allow hiding content inside a script element from any browsers that might otherwise treat that content as phrasing content rather than as JavaScript code.
Script with fallback
(see <script> tag demo above)
Since there are various mechanisms that can disable scripting without the browser being aware of it, the recommended way to provide fallback content for a script is to change from noscript content to scripted content in the script itself, like this:
<head> ... <script type="text/javascript"> window.onload = function() { var elem = document.getElementById('noscript1'); elem.textContent = ""; var button = document.createElement('button'); button.textContent = "Click Me!"; var output = document.createElement('span'); button.onclick = function() { output.textContent = 'Button got clicked!'; return false; } elem.appendChild(button); elem.appendChild(document.createElement('br')); elem.appendChild(output); } </script> ... </head> <body> ... <div id="script-tag-demo"> <p id="noscript1">Scripts failed or are disabled. Please make sure scripting is enabled.</p> </div> ... </body>