You need to be logged in to post in the forums. If you do not have an account, please sign up first.
Problem when using dynamic script tag for loading json data
Hi, i'm trying to use dynamic script tag for loading json data, but on opera 9 it will not work async, so if i have a sleep on the server side it will wait for the response and lock the page so no interaction with buttons etc will not occur. The code for the server side is:
<?php
sleep(10);
echo "pollMsgReply({'message':'Simple message'});";
?>
And on the client side:
<html>
<head>
</head>
<body>
<script type="text/javascript" language="javascript">
// Define the callback function
function pollMsgReply(jsonData) {
alert('jsonData returned');
document.getElementsByTagName("body").item(0).removeChild(document.getElementById("foo"));
}
function JSONscriptRequest(fullUrl,id) {
// REST request path
this.fullUrl = fullUrl;
// Keep IE from caching requests
// Get the DOM location to put the script tag
this.headLoc = document.getElementsByTagName("body").item(0);
// Generate a unique script tag id
this.scriptId = id;
this.scriptObj = document.createElement("script");
// Add script object attributes
this.scriptObj.setAttribute("type", "text/javascript");
this.scriptObj.setAttribute("charset", "utf-8");
this.scriptObj.setAttribute("src", this.fullUrl );
this.scriptObj.setAttribute("id", id);
this.headLoc.appendChild(this.scriptObj);
}
function foo()
{
alert("bar");
}
//
</script>
<input type="button" value="Call foo" onclick="foo()"/>
<script type="text/javascript" language="javascript">
// The web service call
var req = 'http://www.domain.com/getJson.php';
// Create a new request object
var bObj = new JSONscriptRequest(req,"foo");
</script>
</body>
</html>