HTML script
JavaScript makes HTML pages more dynamic and interactive.
Insert a script
How to insert a script into an HTML document.
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <script> document.write("Hello World!") </script> </body> </html>
Run Instance»
Click the "Run Instance" button to view the online instance
Use the <noscript> tag
How to deal with browsers that do not support scripts or have scripts disabled.
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <script> document.write("Hello World!") </script> <noscript>抱歉,你的浏览器不支持 JavaScript!</noscript> <p>不支持 JavaScript 的浏览器会使用 <noscript> 元素中定义的内容(文本)来替代。</p> </body> </html>
Run Instance»
Click the "Run Instance" button to view the online instance
HTML <script> tag
<script> tag is used to define client-side scripts, such as JavaScript. The
<script> element can contain script statements or point to an external script file through the src attribute.
JavaScript is most commonly used for image manipulation, form validation, and dynamic content updating.
The following script will output "Hello World!" to the browser:
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <script> document.write("Hello World!") </script> <noscript>抱歉,你的浏览器不支持 JavaScript!</noscript> <p>不支持 JavaScript 的浏览器会使用 <noscript> 元素中定义的内容(文本)来替代。</p> </body> </html>
Running Example»
Click the "Run Example" button to view the online example
Tip: To learn more about Javascript tutorials, please check out JavaScript Tutorials!
HTML<noscript> tag
<noscript> tag provides alternative content when scripting is not available, such as when scripting is disabled in the browser, or when the browser does not support client-side scripting . The
<noscript> element can contain all elements found within the body element of a normal HTML page.
Only when the browser does not support scripts or scripts are disabled, the content in the <noscript> element will be displayed:
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <script> document.write("Hello World!") </script> <noscript>抱歉,你的浏览器不支持 JavaScript!</noscript> <p>不支持 JavaScript 的浏览器会使用 <noscript> 元素中定义的内容(文本)来替代。</p> </body> </html>
Run instance»
Click the "Run instance" button to view the online instance
JavaScript experience (from this site's javascript tutorial)
JavaScript Example Code
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p> JavaScript 能够直接写入 HTML 输出流中: </p> <script> document.write("<h1>这是一个标题</h1>"); document.write("<p>这是一个段落。</p>"); </script> <p> 您只能在 HTML 输出流中使用 <strong>document.write</strong>。 如果您在文档已加载后使用它(比如在函数中),会覆盖整个文档。 </p> </body> </html>
Run Example»
Click the "Run Example" button to view the online example
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <h1>我的第一个 JavaScript </h1> <p id="demo"> JavaScript 可以触发事件,就像按钮点击。</p> <script> function myFunction() { document.getElementById("demo").innerHTML="Hello JavaScript!"; } </script>
Run Instance»
Click "Run Instance" Button to view online examples
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <h1>我的第一段 JavaScript</h1> <p id="demo"> JavaScript 能改变 HTML 元素的样式。 </p> <script> function myFunction() { x=document.getElementById("demo") // 找到元素 x.style.color="#ff0000"; // 改变样式 } </script> <button type="button" onclick="myFunction()">点击这里</button> </body> </html>
Run Instance»
Click the "Run Instance" button to view the online instance
HTML Script Tag
Tag | Description |
---|---|
##<script> | Definition of client script|
Definition The text output by the script browser is no longer supported |