<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://www.w3.org/Bugs/Public/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4"
          urlbase="https://www.w3.org/Bugs/Public/"
          
          maintainer="sysbot+bugzilla@w3.org"
>

    <bug>
          <bug_id>23509</bug_id>
          
          <creation_ts>2013-10-14 21:25:03 +0000</creation_ts>
          <short_desc>Media-specific scripts (&lt;script media=&quot;&quot;&gt;?)</short_desc>
          <delta_ts>2016-03-16 14:38:29 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WHATWG</product>
          <component>HTML</component>
          <version>unspecified</version>
          <rep_platform>Other</rep_platform>
          <op_sys>other</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>WONTFIX</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P3</priority>
          <bug_severity>enhancement</bug_severity>
          <target_milestone>Needs Impl Interest</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Ian &apos;Hixie&apos; Hickson">ian</reporter>
          <assigned_to name="Ian &apos;Hixie&apos; Hickson">ian</assigned_to>
          <cc>annevk</cc>
    
    <cc>ian</cc>
    
    <cc>mike</cc>
          
          <qa_contact>contributor</qa_contact>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>94710</commentid>
    <comment_count>0</comment_count>
    <who name="Ian &apos;Hixie&apos; Hickson">ian</who>
    <bug_when>2013-10-14 21:25:03 +0000</bug_when>
    <thetext>(The big unanswered question with the proposals below is how to handle changes in the media — e.g. orientation changes, zoom. A solution for that really is needed before we can make progress here.)

On Thu, 16 May 2013, Yoav Weiss wrote:
&gt;
&gt; Arguably, the biggest problem in responsive Web design today is the fact
&gt; that many resource loading mechanisms are media query agnostic, and
&gt; download resources upfront regardless of whether they will be used or not.
&gt;
&gt; One of the biggest resources in question (right after images) is external
&gt; JavaScript. JavaScript also has secondary performance effects, beside its
&gt; download size, since parsing and running it are also time (&amp; battery)
&gt; consuming operations.
&gt;
&gt; Currently authors that want their scripts to load only under certain
&gt; viewport sizes are forced to use JS based script loaders, which have the
&gt; side-effect of preventing these scripts from being parsed out by the
&gt; preloader.
&gt;
&gt; I believe that an HTML based solution would be a better fit, will be easier
&gt; to use by authors, and will enable preloaders to do their job and preload
&gt; these scripts.
&gt;
&gt; Common use-cases for avoiding the load of JS based components based on
&gt; layout are:
&gt;
&gt; * JS based maps, which don&apos;t fit in a narrow layout
&gt; * Social widgets, which don&apos;t fit in a narrow layout
&gt; * Mobile/desktop oriented UI libraries
&gt; * Ad placements
&gt;
&gt; I was thinking along the lines of adding a `media` attribute to the
&gt; `&lt;script&gt;` element.
&gt; Doing that raises several interesting questions:
&gt;
&gt; * What will be the execution order of these scripts if the media conditions
&gt; of the page change?
&gt; * Should `media` be permitted for scripts with no `defer`/`async`
&gt; attributes, that may contain document.write directives?
&gt; * Should scripts re-run if media conditions changed more than once (e.g.
&gt; their `media` intially matched conditions, then stopped, then matched again)
&gt; * Should events handlers that were registered by scripts which media no
&gt; longer matches run?
&gt;
&gt; I believe that mandating `async` (at least as a first step) will enable
&gt; avoiding the running-order problems, with authors being obliged to avoid
&gt; any dependencies between `media` scripts and other (`media` or non-`media`)
&gt; scripts.

On Tue, 21 May 2013, David Bruant wrote:
&gt;
&gt; How bad is the current situation with JS-based loaders? I would expect a
&gt; JS-based loader and the couple of feature-detection tests to be rather small
&gt; as well as heavily cachable, so not that awful for performance.

On Sat, 25 May 2013, Yoav Weiss wrote:
&gt;
&gt; JS based loaders have several drawbacks:
&gt; * JS files are not discovered by the preloader, which means the browsers
&gt; can&apos;t prioirtize their download appropriately.
&gt; * They require JS inlining, which will become significantly harder with CSP
&gt; (JS nonces make it possible, but it&apos;d require server side logic)
&gt; * Arguably, attributes make authoring easier, which may mean authors are
&gt; more likely to avoid useless JS downloads.

On Sat, 25 May 2013, Paul Kinlan wrote:
&gt;
&gt; We developed formfactorjs for exactly this type of case (
&gt; github.com/paulkinlan/formfactor) about 2 years ago.
&gt;
&gt; The use case we had was that we had one app that had to respond differently
&gt; based on the form factor type, so a TV would load js that was specific to
&gt; just the TV (ie loading remote controller logic) and a tablet would loads
&gt; js that responded to finger based touch gestures and a phone type media
&gt; query would load js for thumb based gestures.
&gt;
&gt; We got to the point where were talking about trying to have media queries
&gt; on script element to save us from this library we created, however we
&gt; quickly found that things like orientation changes would mean that there is
&gt; a high degree of chance that the developer would have scripts that would
&gt; need to be loaded at some undeterminable time in the future that would
&gt; clash with the scripts that had already been loaded and ruin the state in
&gt; our app (such as scripts sectioned by min-width queries).
&gt;
&gt; We didn&apos;t find any reasonable solution for this issue other than force a
&gt; reload of the page :/

On Sun, 26 May 2013, Paul Kinlan wrote:
&gt;
&gt; media queries have no logical OR which means if you want one script
&gt; to match multiple media queries (such as one that would match tablet
&gt; and one that would match tv) you need to declare script blocks
&gt; multiple times.

The thread had other feedback too:
   http://goo.gl/otukz7#msg112</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>109145</commentid>
    <comment_count>1</comment_count>
    <who name="Ian &apos;Hixie&apos; Hickson">ian</who>
    <bug_when>2014-07-21 20:52:51 +0000</bug_when>
    <thetext>One option would be to have the media=&quot;&quot; just be a control over when it executes, with nothing special going on when it stops executing.

This could also be integrated into a dependency system: it&apos;s just having scripts depend on a media query, which you&apos;d actually be able to do anyway if you can make a script be a dependency for a style sheet.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>125534</commentid>
    <comment_count>2</comment_count>
    <who name="Anne">annevk</who>
    <bug_when>2016-03-16 14:38:29 +0000</bug_when>
    <thetext>Once there&apos;s a concrete proposal, please open a GitHub issue.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>