View Full Version : Trying to call and external script

07-06-2012, 01:25 AM

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">
<meta charset="UTF-8" />
<meta name="viewport" content="width = device-width" />
<title>Local Twitter Search</title>
function startSearch(){
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;
document.getElementById("results").innerHTML = "<script src='" + query + "'></script>";

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

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.