views:

348

answers:

6

Hi All,

I am calling a ajax method to update a div. It contains links and functions which require java script files. But these methods and functions are not getting called properly as java script files are not getting included through ajax call. For example, i am trying to call a light box function, but it gets redirected to different page and not in light box.

Thanks in advance, Anubhaw Prakash

A: 
T.J. Crowder
Hi Crowder,Thanks for the reply. I tried the code provided in article, but ajax call still does not include js files. It skips the code written under <script> tag.Anubhaw
Anubhaw
@Anubhaw: As I said, you'll have to pre-process the text to find the script tags and isolate the `src` attributes from them. The actual script won't be in the response (of course), it's just *linked to* from that text. (Or if you mean that the tags aren't in the response *at all*, that would be a server-side problem.) The technique in the linked article works fine for adding script files to a page dynamically.
T.J. Crowder
A: 

<script> tags written to innerHTML are not executed at write-time. You can do element.getElementsByTagName('script') to try to get hold of them and execute their scripts manually, but it's very ugly and not reliable.

There are tedious browser differences to do with what happens to a <script> element written to innerHTML which is then (directly or via an ancestor) re-inserted into the document. You want to avoid this sort of thing: just don't write <script>​s to innerHTML at all.

Then you also don't have to worry about executing scripts twice, which is something you never want to do with library scripts. You don't want to end up with two copies of a function/class that look the same but don't compare equal, and which hold hooks onto the page that don't play well with each other. Dynamically-inserted library scripts are a recipe for confusing failure.

Much better to include your scripts statically, and bind them to page elements manually after writing new elements to the page. If you really need to you can have your AJAX calls grab a JSON object containing both the new HTML to add and a stringful of script to execute.

bobince
Hi Bobince,Thanks for the reply. I tried replacing the script tag, but it does not include the js files in ajax request. Can you please elaborate on calling js files dynamically.Anubhaw
Anubhaw
Executing the content of scripts is annoying to do. `eval` of their `textContent` will execute in local scope, which may be no good if you are defining global functions/vars; also you would have to fetch the content of external scripts with XMLHttpRequest. (But then again, you really *don't* want to be defining globals in a script inserted at write time.) A way to execute more reliably is to take the `<script>` element, remove it from the document, `createElement` a *new* `<script>` element, set its `src` or text node content (or `.text` in IE), then insert the new element into the `<head>`.
bobince
The tediousness and fragility of all this should give you a clue: this is not something HTML was designed to do; you are going against the grain trying to hack about with dynamic scripts. For sanity, keep your script code in static files and bind events to new page content manually at write-time or using delegation.
bobince
A: 

May want to try running some prepatory javascript in the :before option to setup a variable with the correct files?

Ray Trask
A: 

The Ajax framework in prototype will properly execute the text content of <script> tags, but will not import new script files via <script src="somefile.js"/>. The only solution I came up with is to import all javascript files I need in the head of the page. That way the functions in the imported file are available to the inline javascript code executed in the Ajax response.

Kevin
A: 
  1. include static scripts on pages that need to use them (IE contain a lightbox, then include the lightbox script)
  2. Problem solved. Do not load scripts using AJAX
  3. Make necessary function calls to the static scripts using AJAX callbacks
Bob Gregor
+1  A: 

I had a similar problem, where I did want to postload some javascript. What I did is separating loading the html-fragment and loading the script into two calls. For loading the script I call the following function (I have JQuery handling the ajax part):

function loadModule(name, callback) {
    $.ajax({type: "POST"
          , url: "/js/" + name
          , dataType: "script"
          , success: callback
    });     
}
Daniel