After much thought and changing my code back and forth, I think I now have a clearer picture, and I would like to share my two cents on this subject before I forget.
<div id='noscript'>show non-js content</div> <script>document.getElementById('noscript').style.display='none';</script> <script id='required script'>show js content</script>
vs
<noscript>show non-js content</noscript> <script id='required script'></script>
Depending on the situation, there are three cases to consider:
Case 1 - If necessary, the script is inline
JavaScript is disabled
- Content in the
<noscript> element appears immediately, non-js content is shown - Content in the
<div> element appears immediately, non-js content is displayed
JavaScript support
- The content in the
<noscript> element is not displayed at all, the displayed js is displayed - Content in the
<div> element may appear momentarily before hiding, js displayed content
For this case, it is useful to use the <noscript> element.
Case 2 - If necessary, the script is from an external (external) source, but hiding the <div> element is performed using the built-in script
JavaScript is disabled
- Content in the
<noscript> element appears immediately, non-js content is shown - Content in the
<div> element appears immediately, non-js content is displayed
JavaScript is enabled, but script is required.
- The content in the
<noscript> element is not displayed at all, nothing is displayed! - The content in the
<div> element may appear for a moment before hiding, nothing is displayed!
JavaScript is enabled and a script is required
- The content in the
<noscript> element is not displayed at all, the displayed js is displayed - Content in the
<div> element may appear momentarily before hiding, js displayed content
For this case, it is useful to use the <noscript> element.
Case 3 - If necessary, the script hides the <div> element
JavaScript is disabled
- Content in the
<noscript> element appears immediately, non-js content is shown - Content in the
<div> element appears immediately, non-js content is displayed
JavaScript is enabled, but script is required.
- The content in the
<noscript> element is not displayed at all, nothing is displayed! - Content is displayed in the
<div> element, non-js content is displayed
JavaScript is enabled and a script is required
- The content in the
<noscript> element is not displayed at all, the displayed js is displayed - Content in the
<div> element may appear momentarily before hiding, js displayed content
For this case, it is useful to use the <div> element.
Finally
Use the <noscript> element if the rendering of HTML content is dependent on third-party scripts or if the required script is inline. Otherwise, use the <div> element and verify that the required script contains:
document.getElementById('noscript').style.display='none';
source share