0

I have installed ads on my website recently and everything works fine in desktop version. I want to change the ads basing on the screen weight, i have ads code like :

<script type="text/javascript>
<script src="file.js">
<iframe>AD CODE </iframe>
</script>

I want to dynamicly change ads from desktop to mobile code basing on screen width using javascript but the problem is when i try to insert code into the ad zone the script 'js' inside will not run and the ad doesnt show.

What is the best practice to do so ?

Thanks to all of you

ibradev
  • 45
  • 11
  • To clarify, you want a script within "file.js" to identify the width of the users screen and if the width is under a certain size to run a script specific to mobiles? – MJ_Wales Jan 22 '19 at 10:07
  • Yes, i want to insert specifi ad for each screen size – ibradev Jan 22 '19 at 10:11

1 Answers1

0

It depends if you want the users screen width or width of the window. If you're trying to determine if the user is using a mobile phone then screen width would likely be most suitable.

var width = window.screen.availWidth;
if(width < 400){
   //do mobile relevant action
} else {
   //do desktop relevant action
};

This question has been answered very thoroughly here: Get the size of the screen, current web page and browser window

MJ_Wales
  • 773
  • 1
  • 6
  • 20
  • Thanks you for aswering, but when i do this way the script file.js inside the code does not run(load) and the ad doesnt show – ibradev Jan 22 '19 at 13:06
  • Can you show the contents of "file.js" within your original question? Also, I'd assume that you would want the iframe tag outside of your script tag. – MJ_Wales Jan 22 '19 at 13:39