24th Apr, 2009

Calling External Javascript after Ajax Call [Mootoools]

If you are trying to run the script in an updated div, that may work with the internal scripts - something like;

<script type=”text/javascript”>
alert(’hello’);
</script>

But when you try to access some external javascrips, (e.g);

<script type=”text/javascript”>
<script type=”text/javascript” src=”/javascripts/my_script.js”></script>
</script>

that will not work.

The work-around is;

[if you are using mootools.js for making ajax calls]  /  for other frame-works, check how to set ‘evalScripts’ param

1) First step is to, add

“evalScripts: true” param

(E.g)

new Ajax(url, {
method: ‘get’,
update: $(’content’),
evalScripts: true
}).request();

2) Then in the div, make the javascript look like;

<script type=”text/javascript”>
new Asset.javascript(’/javascripts/my_script.js’);
</script>

(or)

Load the javascript dynamically like this; [if 'Asset.javascript' is not supported]

<script id=”external_script” type=”text/JavaScript”></script>
<script>
document.getElementById(’external_script’).src = ‘/javascripts/my_script.js’;
</script>

Responses

Thanks for writing this.

Leave a response

Your response:

Categories and Tags

Advertising