Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    WLC
    WLC is offline
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Trying to call and external script

    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?

    Code:
    <!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.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,985
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •