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'

Barmar
  • 741,623
  • 53
  • 500
  • 612
Karthik
  • 83
  • 1
  • 1
  • 7
  • What about it doesn't work? Have you tried putting in `alert()` calls to ensure the script is running? – Niet the Dark Absol Dec 09 '15 at 21:02
  • 6
    Typo here? `sr.src` – isherwood Dec 09 '15 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. – Karthik Dec 09 '15 at 21:11
  • Do you get this error in all browsers or only in 1 or 2 like IE? – Anke Wenz Dec 09 '15 at 21:14
  • @AnkeKnicker I tested on forefox and chrome it is consistent. I feel there is some timing issue. I have a script after the above script element which makes a function call on the object from the above script. It works if I run in debug mode or put some `alert("test")` in the above script. But if there is no break point/ no alert statement the function calls are not working. – Karthik Dec 09 '15 at 21:21
  • Do you get any errors in developer console – Jaromanda X Dec 09 '15 at 21:36
  • @JaromandaX I am not seeing any errors, and I have edited my code above please see if that helps. – Karthik Dec 09 '15 at 21:49
  • 1
    How do you set `server`? – Barmar Dec 09 '15 at 21:58
  • I suspect this is related to an asynchronous function that you haven't shown. When you stop at the breakpoint, that allows the async function to complete and set the variable. – Barmar Dec 09 '15 at 21:59
  • server is undefined, so only test.js will ever load (given the code shown). sample will have to be defined in both test.js and sample.js. In "not available" an error you are seeing, or your description of the problem? – MatthewMartin Dec 09 '15 at 22:03
  • @Barmar `server` is set using url parameters, it is being set appropriately. I tried to put `console.log(stmt)` inside my script and it always print the stmt to the console. – Karthik Dec 09 '15 at 22:14
  • What is `stmt` and what does it have to do with the above code? – Barmar Dec 09 '15 at 22:17
  • @Barmar I have just added some logging statements to the above code, that is what I am trying to tell. I added logging statements and it prints those statements to the console. – Karthik Dec 09 '15 at 22:20
  • As @isherwood asked before: Is `sr.src` a typo for `sc.src`? Is that in the real code or a copying error? – Barmar Dec 09 '15 at 22:22
  • 1
    The problem is that the script is loaded asynchronously. See the duplicate question for how to deal with this. – Barmar Dec 09 '15 at 22:24

0 Answers0