How to load file. JS depending on the width of the screen

Asked

Viewed 41 times

2

I have a file .js who would like to upload only to Desktop version, when it is on more narrow screens would like the request for that .js were not done, or even another was carried .js. Like, for large screens > 768px charges desktop.js, and for screens < 786px charges mobile.js

I know that for CSS can be done like this:

<link rel="stylesheet" href="css/mobile.css"  media="screen and (max-width: 767px)" />
<link rel="stylesheet" href="css/desktop.css" media="screen and (min-width: 768px)" />

And for images we can do so for example:

<picture>
  <source srcset="desktop.jpg" media="(min-width: 768px)">
  <source srcset="mobile.jpg"  media="(max-width: 767px)">
  <img srcset="mobile.jpg" alt="…">
</picture>

Then the doubt is, has to do something similar to file requests .js depending on the screen width?

  • Man I tested and it worked! But if it is with the simulator of Chrome Devtools not right, to see working has to decrease in hand the Browser window. Thanks to the tip!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.