0

I'm trying to include a couple of conditions in my code excluding only android visitors. I put this code but the first script in else condition is not working.

    <script type='text/javascript'>
if( /Android/.test(navigator.userAgent) ) {
 // some code..
}else
{
<script type="text/javascript" src="link"></script>
<script type="text/javascript" src="link"></script>
}

</script>
John Grischam
  • 224
  • 1
  • 19

2 Answers2

1

You can't wrap raw html in an else

You could add them using document.write()

Something like:

if( /Android/.test(navigator.userAgent) ) {
 // some code..
}else
{
   document.write('<script type="text/javascript" src="link"></script>');
   document.write('<script type="text/javascript" src="link"></script>');
}
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • Thank you for your answer. I'm trying your method but still the first script is not active. The second script works properly. – John Grischam Jul 16 '18 at 18:01
  • Well if one works no reason the other shouldn't. Whatever isn't working is probably unrelated to writing the script tags into the document – charlietfl Jul 16 '18 at 18:03
  • i think is a writing error and not a script problem because when i test the script alone it works properly. – John Grischam Jul 16 '18 at 18:08
  • what is even stranger is that when i change the ranking of the scripts (put the first one after the second script (so the actual second changes to first position) the script not working before works now and the one going up does not work anymore – John Grischam Jul 16 '18 at 18:20
  • That sounds more like a problem with what is actually in the scripts – charlietfl Jul 16 '18 at 18:22
  • So i did this and it works but is so bad :"); document.write(""); } "); document.write(""); } – John Grischam Jul 16 '18 at 18:24
0

You can't do that.

The easiest way to do what you want to achieve is to include all scripts and then execute its content conditionally.

If you can't control the source code of these scripts, then this is a duplicated question of How do I include a JavaScript file in another JavaScript file?

lilezek
  • 6,976
  • 1
  • 27
  • 45