PDA

View Full Version : External Javascript file not loading initially



bkudrle
13th March 2009, 17:05
I just started playing around with Qt 4.5 on the Mac and am interested in the WebKit integration. I found something interesting about the Previewer example application (under examples->webkit->previewer). If the javascript is inline, I can load it in the text window successfully and click the Preview button and the javascript works successfully. However, if I try to reference an external javascript file instead of having the javascript inline, then the javascript will not be loaded. However, if I open the file with the same content from the menu (File->Open) then it WILL load the external javascript file successfully when I click the Preview button. Interesting behavior...

Below is a sample of the HTML & Javascript code that I have been using for testing.

<html>
<header>
<script language="Javascript">
function showToday() {
var today = new Date();
alert(today);
}
</script>

<script type="text/javascript" src="test.js"></script>
</header>
<body>
Hello World
<form>
<input type="button" value="Current Date and Time" onClick="javascript:showToday()">
</form>
</body>
</html>

The test.js file is an external file with just the showToday function in it.

So in other words, if I copy and paste the above code in the text edit pane of the Previewer app and click the Preview button and then click the "Current Time and Date" button, I get the alert box for the current date. If I comment out the inline Javascript and do the same, then I do not get any alert box. However, even with the inline javascript commented out, then if I do a File->Open from the menu and load a file with the same code (inline Javascript is commented out), then when I click the Preview button and then the "Current Time and Date" button, then I WILL get the alert box. In other words, in this second case it DID load the external test.js file. So there is something about the File->Open that kind of re-sets something that makes the WebView actually load external JS files. Anyone have any idea what is going on or how to get around this (i.e., I would like to be able to load external JS files on startup).