简体   繁体   中英

How to call an external js file <script src="path_to_file/file_name.js"> for screen size less than 768px?

I have a file with php Yii code, designed using bootstrap. In that, I require a particular external js file only for screen width 768px or less. How can I call the external js file for screen size less than 768px?

I have tried:
<div class="show-xs"> <script src="path_to_file/file_name.js"> </div>
Where the div is styled "display:none" upto 768px and "display:block" below 768px

Verified and working!

<script>
if (screen && screen.width < 768) {
  document.write('<script type="text/javascript" src="path_to_file/file_name.js"><\/script>');
}
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM