0

To make a responsive website I disable some content in the mobile versions (or add content in the desktop/tablet version) Now that I disabled the content it is not nescessary to include certain javascript files. This will save bandwidth for my mobile page.

Is this possible?

broersa
  • 1,488
  • 2
  • 15
  • 27

2 Answers2

2

Yes you can use of advanced JavaScript libraries freely available on the internet. One of them is http://requirejs.org/ which is used for conditionally loading scripting files and other resources. Or you can also use Modernizr to check for media queries and write your desired script into the blocks.

Umair Hafeez
  • 397
  • 2
  • 8
  • As far as I can see RequireJs doesn't load conditionally on screen width on other css media queries – broersa Mar 21 '14 at 08:45
  • 1
    Yes true, but as I told earlier, you can load scripts on the run so, simply you can write your own logic to check the screen size and load the scripts accordingly. Or you can use something like Enquire.js to help you doing this too. – Umair Hafeez Mar 21 '14 at 09:40
0

try this

<script type="text/javascript">

    if (screen_width < 1000) { 

        document.write('<script type="text/javascript" src="javascriptFile.js"></script>');  
     } 

</script>
WhiteLine
  • 1,649
  • 1
  • 19
  • 48
  • `document.write()` is deprecated for years and should not be used. – ElmoVanKielmo Mar 21 '14 at 08:19
  • Do you have an alternative? As far as I can see RequireJs doesn't load conditionally on screen width. – broersa Mar 21 '14 at 08:44
  • It's exactly like @Umair says in his answer and comment. `document.write()` also doesn't do any check for screen width. It's up to developer to write the logic for this. Trust me, nobody would deprecate `document.write()` if it solved any problem which couldn't be solved in a better way. Do I really have to explain why it is deprecated? – ElmoVanKielmo Mar 21 '14 at 14:57
  • @ElmoVanKielmo Where did you get the idea [`document.write`](https://developer.mozilla.org/en-US/docs/Web/API/document.write) is deprecated? It's perfectly acceptable to use, if used correctly, as the above code does. See [HTML5Boilerplate's template](https://github.com/h5bp/html5-boilerplate/blob/master/index.html) for a similar example. – Blazemonger Mar 21 '14 at 18:02
  • @Blazemonger just google for `document.write deprecated`. Why don't you tell the truth: It introduces a performance hit, it may break the page loading if not used properly, you should use DOM manipualtion to insert DOM nodes, you can load JS conditionally using @Umair answer for OP question. – ElmoVanKielmo Mar 22 '14 at 10:17
  • @ElmoVanKielmo [well, look what Google returned](http://stackoverflow.com/questions/12574098/is-document-write-actually-deprecated). Yes, you should use DOM manipulation when appropriate, but if you *look at how it's used in these examples,* where it *is* used properly, you'll see they aren't even about DOM manipulation. – Blazemonger Mar 22 '14 at 23:07
  • And why use a plugin when you can use two lines of vanilla JS to get the same result sooner? I think @ElmoVanKielmo misunderstands what 'deprecated' actually means. – Blazemonger Mar 23 '14 at 02:24