Paul
Paul

Reputation: 227

Adding a javascript tag only in mobile devices

I need a little help with getting my javascript code to run only in mobile devices. I did some simple research and found this.

<script type="text/javascript">
   if($(window).width() <= 480){
    // do your stuff
   }
</script>

but when I implement it with my code, its not working right.

<script type="text/javascript">
  if($(window).width() <= 480){
    name='container' src='https://example.com/embeds/java.js'
   }
</script>

Is there something I am missing?

Upvotes: 0

Views: 308

Answers (1)

utsav
utsav

Reputation: 622

you also have to check height to get exact device dimensions

<script type="text/javascript">
var width = $(window).width(), height = $(window).height();
    if ((width <= 1023) && (height >= 768)) {
      //do some stuff
    }
</script>

Upvotes: 1

Related Questions