How do I dynamically load a js file using Prototype?

Posted by domagoj412 on Stack Overflow See other posts from Stack Overflow or by domagoj412
Published on 2009-04-05T16:49:33Z Indexed on 2010/05/13 18:44 UTC
Read the original article Hit count: 227

Filed under:
|
|
|

Hello,

I am using prototype to load external js file (actually it is php file) dynamically. Like this:

function UpdateJS(file)
{
  var url    = 'main_js.php?file='+file;
  var myAjax = new Ajax.Request( url, {method: 'get', onComplete: showResponseHeader} );
}
function showResponseHeader (originalRequest) 
{
  $('jscode').innerHTML = originalRequest.responseText;
}

Container "jscode" is defined like this:

<script type="text/javascript" id="jscode"></script>

And it works! But if some different file is called, all the functions from previous one are preserved. And I don't want that. Anybody knows how to "unload" first js file when second one is called?

(I also tried using Ajax.Updater function but the result is the same.)

Update: It turns out that there is bigger problem: it only loads if function "UpdateJS" is in window.onload that is why it doesn't load anything else after that. So prototypes update it's maybe not such a good way for this...

© Stack Overflow or respective owner

Related posts about dynamically

Related posts about JavaScript