PDA

View Full Version : Trying to call and external script



WLC
07-06-2012, 01:25 AM
Hello,

I'm trying to have a webpage display a result from an external script. The URL for the external script is generated by the searchTwitter() function. I would expect the result to be displayed.

Right now I see nothing. I tried appending the script to the header and to the division with id = “results.” I also tried changing the innerHTML attribute of the “results” division. What am I doing wrong?



<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width = device-width" />
<title>Local Twitter Search</title>
<script>
<!--//--><![CDATA[//><!--
function startSearch(){
searchTwitter();
}
function searchTwitter(){
var query = "http://search.twitter.com/search.json?callback=showResults&q=";
query += document.getElementsByName("kwBox")[0].value;
var script = document.createElement("script");
script.src = query;
script.onload=scriptLoaded;
alert(document.getElementsByTagName("head")[0].getElementsByTagName("script").length);
document.getElementsByTagName("head")[0].appendChild(script);
alert(document.getElementsByTagName("head")[0].getElementsByTagName("script").length);
document.getElementById("results").innerHTML = "<script src='" + query + "'></script>";

document.getElementById("results").appendChild(script);
alert("script appended to 'results'");
}
function scriptLoaded() { alert("script loaded"); }
//--><!]]>
</script>
</head>
<body>
<div id="main">
<label for="kwBox">Search Twitter: </label>
<input type="text" id="kwBox" />
<input type="button" value="Go!" onclick="startSearch()" />
</div>
<div id="results">
</div>
</body>
</html>


I am new to javascript and this is a modification of an example I pulled off the web.

Philip M
07-06-2012, 08:38 AM
For security reasons JavaScript cannot access anything beyond the domain of the current page. This is known as the "same origin policy" and prevents a document or script loaded from one origin from getting or setting properties of a document from a different origin.

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.