Make WordPress Core

Opened 8 years ago

Closed 8 years ago

#38242 closed enhancement (wontfix)

Support media queries in wp_enqueue_script() akin to wp_enqueue_style() to prevent loading scripts unnecessary

Reported by: bjornw's profile BjornW Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.7
Component: Script Loader Keywords: close
Focuses: Cc:


I'd like to suggest wp_enqueue_script() to support the option to load scripts based on media queries similar to wp_enqueue_style().

This would allow us to prevent scripts from loading unless a certain media query has been met.

See also these links for a bit more background:

Change History (2)

#1 @peterwilsoncc
8 years ago

  • Keywords close added

The <script> tag doesn't support media queries. window.matchMedia() can be used by developers to detect media queries but only inside the script.

A lot of JavaScript code would need to be written to support this, I think it falls outside the scope of wp_enqueue_script().

#2 @dd32
8 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

As I said in #core @ I don't think this is really going to be viable for core inclusion.

Conditionally loading JS based on media queries seems like something that's either best done by a helper script (even using wp_add_inline_script()), or as a feature detection within the script itself on whether or not to spend time executing certain procedures.
One big question in my mind, is what happens when the window resizes? or rotates as in mobile devices, are those scripts then loaded at that point in time? What happens to existing ones which matched the query, but no longer do?

So although I like the suggestion, as long as browsers don't natively support it, I think it's best left up to the JS itself to handle conditional loading.

I'm going to mark this as wontfix for the reasons above, even though I like it, hopefully one day either this sort of thing won't be needed, or browsers support it natively.

Note: See TracTickets for help on using tickets.