Me thinks you should write
- if ( typeof jQuery === undefined )
Without the double quotes.
I suppose here that when you append script_tag it is an asynchronous process, which is why you define .onload().
As far as I can see if you define $(document).ready() right after the if.then.else then jQuery is not yet loaded, so it will trigger an error.
I would define $(document).ready() in main ();
Keep us posted, I am interested.