A web developer’s diary

October 20, 2006

Object.returnHTML and Javascript evaluation

Filed under: Ajax — Celia @ 4:02 pm

One of my friends had a doubt and she had mailed me this.

Question:
In one of my pages, I am using ajax to show dynamic content. I am making an ajax call and using the innerHtml to show the content returned. The problem here is, my returned content has some javascript code which has to be executed. But its not. InnerHtml simply writes without executing any js. Is there any function which will execute the js in my content?

I replied to her and at the same time thought that it would be useful to my blog readers as well.

Answer:
Unfortunately, No. While handling Ajax calls and assigning the returned content to returnHTML, the returned javascript does not get evaluated by default. You should explicitly evaluate it. And, how do you do that? Assign an id to your script tag and evaluate the javascript content within this script tag using eval() function.

For people, who think that this is complicated, there are some nice ajax libraries out there like prototype and script.aculo.us. You can use their functions and all you would need to do there is to set a boolean variable called evalScripts to true. Many popular sites like Digg and Tezaa use these libraries.

Let me know if this helped. If you found a better solution, let me know too.

Advertisements

5 Comments »

  1. Wow! We can also use this:
    var scr = document.createElement(“script”);
    scr.text = “script”; // Script of Page
    document.body.appendChild(scr);

    Works fine!

    Comment by JulioGreff — October 22, 2006 @ 10:06 pm | Reply

  2. Thats great !!

    Comment by Bindu — October 28, 2006 @ 9:29 am | Reply

  3. deposit free bonus casino
    see to signature…

    Comment by casinosfreebonusesv — August 12, 2007 @ 8:00 am | Reply

  4. popular free ringtones

    http://www.thehotstop.info

    signature…

    Comment by freeeeringtones — August 18, 2007 @ 6:52 am | Reply

  5. louise glover pics

    Comment by counterstr — February 7, 2008 @ 9:34 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: