I'm trying to use document.write()
to load the Google Libraries JavaScript API. The following works (the alert pops up) in every browser other than IE. Please note that I replaced my actual API key with "MY_API_KEY" in the example below and that the script tag is broken into two strings to keep the browser from having parsing errors.
<head>
<script language="javascript" type="text/javascript" src="load.js"></script>
</head>
<body>
where the contents of load.js are:
document.write('<scr'+'ipt type="text/javascript" language="javascript" \
src="http://www.google.com/jsapi?key=MY_API_KEY"></scri'+'pt>');
document.write('\
<scr'+'ipt type="text/javascript" language="javascript"> \n \
if (window.google && window.google.load) { \n \
alert("google has loaded"); \n \
} \n \
</scri'+'pt> \
');
If I move the Google Libraries API script tag out of load.js to a script tag in the header, i.e.,
<head>
<script type="text/javascript" language="javascript" src="http://www.google.com/jsapi?key=MY_API_KEY"></script>
<script language="javascript" type="text/javascript" src="load.js"></script>
</head>
<body>
Then the code works in IE. Any idea what's going on and how I can get the first example to work in IE? I'm basically trying to load a number of different JavaScript files from one external JavaScript (*.js) file - I've done this in the past for other JavaScript files and it works, even in IE, but there is something about the Google API script that doesn't work in this scenario when using IE.