topical media & game development
professional-ria-03-DynamicScripting.htm / htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head id="HD">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Yahoo!Traffic API Call. JSON. No Proxy</title>
<script type="text/javascript" src="../../scripts/jsr_class.js"></script>
<script type="text/javascript">
//<![CDATA[
var __appid = "YahooDemo";
var __location = null;
var __server = "http://api.local.yahoo.com/MapsService/V1/trafficData";
var __req = null;
function getTraffic(jData) {
if (jData.ResultSet.Result.length ==0){
document.getElementById('result').innerHTML = "Sorry, No Traffic Data";
return;
}
var lat = jData.ResultSet.Result[0].Latitude;
var lon = jData.ResultSet.Result[0].Longitude;
var title = jData.ResultSet.Result[0].Title;
var descr = jData.ResultSet.Result[0].Description;
var severity = jData.ResultSet.Result[0].Severity;
var type = jData.ResultSet.Result[0].type;
var report = "<h2>" + title + "</h2><br /><hr/>"
+ "Lat/Lon: " + lat + "/"+ lon + "<br/>"
+ "Type: " + type + "<br/>"
+ "Severity: " + severity + "<br />"
+ "Details:" + descr;
document.getElementById('result').innerHTML = report;
}
function getTrafficReport(){
if (__req != null){
aObj.removeScriptTag(); // clean up the DOM
}
var __req = __server + "?appid=" + __appid
+ "&location="+document.getElementById("location").value
+ "&output=json&callback=getTraffic";
alert("getTrafficReport :"+ encodeURI(__req));
aObj = new JSONscriptRequest( encodeURI(__req));
aObj.buildScriptTag();
aObj.addScriptTag();
}
//]]>
</script>
</head>
<body>
<div id="header">
<h1>JSON Dynamic Script Approach for Web Service Requests</h1>
<h2>a SCRIPT section is dynamically injected into the page-as-application's HEAD</h2>
</div>
<div class="inputelement">
<button onClick="getTrafficReport()">Get Traffic Report</button>
<input id="location" value="1300 N. 17th St., Arlington, VA 22209" size="50"></input>
</div>
<br />
<div id="result"></div>
</body>
</html>
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.