Skip to content

Instantly share code, notes, and snippets.

@raulb
Created July 12, 2012 21:18
Show Gist options
  • Save raulb/3101058 to your computer and use it in GitHub Desktop.
Save raulb/3101058 to your computer and use it in GitHub Desktop.
Retrieving source code using XMLhttpRequest with JS
  var req = new XMLHttpRequest();
  req.open(
      "GET",
      "URL",
      true);
  req.onreadystatechange = statusListener;
  req.send(null);

  function statusListener()
  {
    if (req.readyState == 4) 
    {
        
      if (req.status == 200) {

        var code = req.responseText;

        var s = document.createElement('script');
        s.type = 'text/javascript';
        
        try {
          s.appendChild(document.createTextNode(code));
          document.body.appendChild(s);
          // do whatever you want with your js recently loaded;

        } catch (e) {
          s.text = code;
          document.body.appendChild(s);
        }
      }
      
    }
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment