0

Just a simple question. (probably really basic I quess)

Using XmlHttp I am able to load a page, but not sure how to iclude the js files..

How can I alter the code in the images below, to be able to include the two java script files into the page loaded with ajax? I'm by far not a java script programmer, so really needs some help.

Thank you.

clientside.php:

<html>
<head>
<title></title>
  <script src="XmlHttp.js"></script>
  <script>
    var urlServer ="serverside.php";
    var xmlObj = null;

    function callServer(urlServer){
      xmlObj= XmlHttp.create();
          if(xmlObj) {
            xmlObj.onreadystatechange = responseFromServer;
            xmlObj.open("GET", urlServer, true);
            xmlObj.send();
          }
    }       

    function responseFromServer() {
      if (xmlObj.readyState == 4) {
        if (xmlObj.status == 200) {
          document.getElementById("main").innerHTML = xmlObj.responseXML.getElementsByTagName("mybody")[0].firstChild.nodeValue;
          eval(xmlObj.responseXML.getElementsByTagName("myscript")[0].firstChild.nodeValue);                
        }
      }
    }   
  </script>
</head>
<body>
  <div id="main"></div>
  <input type="button" value="Begin test" onClick="callServer(urlServer)">
</body>
</html>



serverside.php :

<?php
  //How do I load these two javascript files together with the <body> below.
  //Obviously the method below does not work..
  $script="
   <script type='text/javascript' src='SlimBox/js/mootools.js'></script>
   <script type='text/javascript' src='SlimBox/js/slimbox.js'></script>
  ";

  $body="
    <body>
    <a href='img/facebook-icon.png' rel='lightbox[roadtrip]' cap='test'><img alt=''border='0' src='img/facebook-icon.png' /></a>
    </body>
  ";

  //Now build the xml page:
  $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
  $xml .= "<mypage>";
  $xml .= "<myscript>".htmlentities($script)."</myscript>";
  $xml .= "<mybody>".htmlentities($body)."</mybody>";
  $xml .= "</mypage>";
  header('Content-type: text/xml');
  echo $xml;
?>
4
  • Why not just include the scripts in the calling page? Commented Jan 15, 2012 at 21:31
  • Are u upen using jQuery? Commented Jan 15, 2012 at 21:33
  • for some reason not working from the calling page.. unless I'm doing something wrong ofcourse.. Commented Jan 15, 2012 at 21:36
  • Jquery sure I guess.. Just want this to work :) Commented Jan 15, 2012 at 21:38

1 Answer 1

1

most efficient way will be to use ajax to load script source(uri) and use javavcript to update script source

Something like:

 document.getElementById('external_script').src = 'http://cross.domain.com/other.js';

Fore more on how to load script dynamically check

Sign up to request clarification or add additional context in comments.

5 Comments

If I use your suggestion, do I add this line above "document.getElementById("main").innerHTML", and then create a <div> called 'external_script', or am I misunderstanding here. Couldn't get it to work.. :(
I was thinking of creting a script with empty source in your main page and then modify the src of that script
sorry for stupid questions here... a empty script like this "<script src=""></script>"?? but what does the name 'extrnal_script' refer to?
referes to the id of that script, so that it can be accessed later in code. "<script src="" id="extrnal_script"></script>"
Thank you. I did as you described, but I only got it to work with a js file I created myself and using "<a href='img/facebook-icon.png' onclick='call_to_func()'" instead of "rel=". But when trying to use the 'slimbox.js' and 'mootools.js' files mentioned above, with "rel=" it does not work. Why is this different?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.