简体   繁体   中英

JS for screen width

How do you load a file based on the browser screen size? This is the code I'm using:

<script>
if(iPhone) { <- screen size instead of device
  document.write("<meta name="viewport" content=\"width=480\""+"/>");
} else {
  document.write("<meta name="viewport" content=\"width=1024\""+"/>");
}
</script>

Thanks

You could try:

<script>
    if (window.screen.width < 481) {
        document.write("<meta name="viewport" content=\"width=480\""+"/>");
    } else {
        document.write("<meta name="viewport" content=\"width=1024\""+"/>");
    }
</script>

The code for screen size is simple as it is just window.screen.width and window.screen.height . I would not recommend targeting a website towards a specific device unless you plan to design the website with the same design patterns as that website.

You could write your own Javascript using document.body.offsetWidth.

eg if(document.body.offsetWidth < 1024){...}

Or use a library like: http://responsejs.com/

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