Forum Moderators: open

Message Too Old, No Replies

Content Fetched by Ajax Not Recognizing Javascript

         

Titanium411

6:23 am on Jan 5, 2009 (gmt 0)

10+ Year Member



Hi everybody,

I have a rather simple problem that I for some reason (probably because I am new to coding) can not resolve. I have a template which includes all the necessary javascript and css files. I use ajax to fetch content and use innerhtml to insert the content into a div tag (since the content is in html). The content inserted in the div include links with id's, that when clicked, are used to fetch further content via ajax. However, the event that is supposed to be triggered by the click is not occurring, which seems to be due to the fact that the ajax fetched content doesnt appear to recognize the javascript files in the template.

My question is how can I get the newly inserted content to recognize the javascript tags and code in the template?

Any help would be greatly appreciated.

daveVk

7:02 am on Jan 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



innerHTML will not work as it excludes the javascript

if you happy to have the results got to an iframe you can use document.write. You could use a hidden iframe as staging post, then move its html to a div.

Otherwise extact javascript and eval() it may be an option.

Titanium411

9:52 am on Jan 5, 2009 (gmt 0)

10+ Year Member



Thanks for the response. I tested a couple things and found interesting results. If I use inline javascript, everything works fine. However, when I try to use external javascript (Im using mootools), that is where the problem arises.

Example:

External (Doesnt Work)
$('getfunction').addEvent('click', function(event){ function hello()...})

<a id="getfunction">

Inline (Works)

<a onclick="javascript: hello(); return false">

Any idea why this may be the case?

daveVk

12:11 pm on Jan 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you do an alert( div.innerHTML ) all script tags and internals are missing whereas <a onclick="javascript: hello(); return false"> survives untouched.

I dont understand the rules, you can access the javascript within the head section via dom functions, I dont think you can for script within the body (within script tags) as for the anon functions such as the onclick= stuff you can get to it via element.onclick.

If you have control of all the source, then more all the javascipt into the head section, if you are dealing with random html from some other source life is more interesting.

Welcome to forum.