Day 106: the scripting media feature

The scripting media feature is an excellent addition to CSS for those who believe in progressive enhancement: It enables you to detect whether scripting languages, such as JavaScript, are supported.

If you disable Javascript in Firefox 117+, Chrome 120+, or Safari 17+ on this page, the disclosure widget below hides the button and displays the content instead.

Detailed content goes here…

CSS

@media (scripting: enabled) {
  .disclosure-content {
    display: none;
  }
}

@media (scripting: none) {
  button[aria-expanded] {
    display: none;
  }
}

HTML

<div class="disclosure">
  <button aria-expanded="false">
    Show details
  </button> 

  <div class="disclosure-content" id="content">
    <p>Detailed content goes here…</p>
  </div>
</div>

JS

const button = document.querySelector('button');

button.addEventListener('click', e => {
  button.setAttribute('aria-expanded', button.getAttribute('aria-expanded') === "false")
})

PS: Yes, I know, it’s better not to ship the button in the first place instead of hiding it.

My blog doesn’t support comments yet, but you can reply via blog@matuzo.at.


This content originally appeared on Manuel Matuzović - Blog and was authored by Manuel Matuzović

The scripting media feature is an excellent addition to CSS for those who believe in progressive enhancement: It enables you to detect whether scripting languages, such as JavaScript, are supported.

If you disable Javascript in Firefox 117+, Chrome 120+, or Safari 17+ on this page, the disclosure widget below hides the button and displays the content instead.

Detailed content goes here…

CSS

@media (scripting: enabled) {
  .disclosure-content {
    display: none;
  }
}

@media (scripting: none) {
  button[aria-expanded] {
    display: none;
  }
}

HTML

<div class="disclosure">
  <button aria-expanded="false">
    Show details
  </button> 

  <div class="disclosure-content" id="content">
    <p>Detailed content goes here…</p>
  </div>
</div>

JS

const button = document.querySelector('button');

button.addEventListener('click', e => {
  button.setAttribute('aria-expanded', button.getAttribute('aria-expanded') === "false")
})

PS: Yes, I know, it's better not to ship the button in the first place instead of hiding it.

My blog doesn't support comments yet, but you can reply via blog@matuzo.at.


This content originally appeared on Manuel Matuzović - Blog and was authored by Manuel Matuzović


Print Share Comment Cite Upload Translate Updates
APA

Manuel Matuzović | Sciencx (2023-11-13T00:00:00+00:00) Day 106: the scripting media feature. Retrieved from https://www.scien.cx/2023/11/13/day-106-the-scripting-media-feature/

MLA
" » Day 106: the scripting media feature." Manuel Matuzović | Sciencx - Monday November 13, 2023, https://www.scien.cx/2023/11/13/day-106-the-scripting-media-feature/
HARVARD
Manuel Matuzović | Sciencx Monday November 13, 2023 » Day 106: the scripting media feature., viewed ,<https://www.scien.cx/2023/11/13/day-106-the-scripting-media-feature/>
VANCOUVER
Manuel Matuzović | Sciencx - » Day 106: the scripting media feature. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/11/13/day-106-the-scripting-media-feature/
CHICAGO
" » Day 106: the scripting media feature." Manuel Matuzović | Sciencx - Accessed . https://www.scien.cx/2023/11/13/day-106-the-scripting-media-feature/
IEEE
" » Day 106: the scripting media feature." Manuel Matuzović | Sciencx [Online]. Available: https://www.scien.cx/2023/11/13/day-106-the-scripting-media-feature/. [Accessed: ]
rf:citation
» Day 106: the scripting media feature | Manuel Matuzović | Sciencx | https://www.scien.cx/2023/11/13/day-106-the-scripting-media-feature/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.