In HTML pages, if a user's browser has disabled JavaScript, you can use the <noscript> tag to provide an alternative message. For example:
html<!DOCTYPE html> <html> <head> <title>How to Detect if JavaScript is Enabled</title> </head> <body> <!-- If JavaScript is enabled, the content below will be displayed --> <script type="text/javascript"> document.write("JavaScript is enabled!"); </script> <!-- If JavaScript is disabled, the content within the `<noscript>` tag will be displayed --> <noscript> <p>JavaScript is disabled. Some features of this website may not work properly; please enable JavaScript.</p> // or <meta http-equiv="refresh" content="0; url=whatyouwant.html" /> </noscript> </body> </html>
The example above demonstrates that when JavaScript is disabled, the content within the <noscript> tag is displayed. If JavaScript is enabled, the code inside the <script> tag is executed, and the page shows the message 'JavaScript is enabled!'.
Please note that using the <noscript> tag is merely a simple way to inform users and is not a programmatic detection method. If you need to detect whether JavaScript is disabled on the server side, you typically need to use a fallback method that does not rely on JavaScript, such as determining it through form submissions. This is because without JavaScript support, asynchronous JavaScript requests (e.g., using Ajax) will not occur, and you can only rely on traditional synchronous requests.