0

I am conditionally loading JavaScript and it seems like it doesn't run when I run it normally. But when I run the same with debug mode with breakpoint. It works fine. Could anyone please let me know what is the issue?

I have this JavaScript in header of the html.

<script> var sc = document.createElement('script'); sc.type = 'text/javascript'; if(server == "a"){ sc.src = "sample.js"; } else{ sr.src = "test.js"; } document.getElementsByTagName('head')[0].appendChild(sc); </script> <script type='text/javascript'> sample.getSomething(); </script> 

This sample object is an object in sample.js/test.js and gets loaded only in debug mode, otherwise it is 'not available'

15
  • What about it doesn't work? Have you tried putting in alert() calls to ensure the script is running? Commented Dec 9, 2015 at 21:02
  • 6
    Typo here? sr.src Commented Dec 9, 2015 at 21:03
  • @NiettheDarkAbsol I tried to put console.log(stmt) and it prints the stmt. When I said it doesn't work, I mean I feel script is not being loaded to head. Commented Dec 9, 2015 at 21:11
  • 1
    How do you set server? Commented Dec 9, 2015 at 21:58
  • 1
    The problem is that the script is loaded asynchronously. See the duplicate question for how to deal with this. Commented Dec 9, 2015 at 22:24

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.