how can i make the javascript that is in pages called using ajax work after the call has been made and they page has been loaded?
thanks
i searched but didnt find anything relevant
| SitePoint Sponsor |
how can i make the javascript that is in pages called using ajax work after the call has been made and they page has been loaded?
thanks
i searched but didnt find anything relevant
any help?





I don't get you. You wanna make an Ajax request after the page loaded? Use window.onload.
No, i want the javascript that is in the content that was called by ajax to work

On many browsers, this won't work.
You must run the script manually after you set the HTML of the content.
You should seperate the content and the JavaScript.
I would do like this:
So when a response arrives, set the HTML content, and then run the JS code, either by using `eval('code here')` or `(new Function('code here'))()`.Code:<Page> <HTML><![CDATA[ Contents should go here. No JavaScript codes should be here. ]]></HTML> <JS><![CDATA[ // JavaScript code goes here. ]]></JS> </Page>




you need to use eval, some browsers set the rule strict, <script> tag will not be executed if you use innerHTML to update the content.
I'm using ajax and responseText to populate innerHTML of a div. I had this problem and came up with this solution to execute the javascript.
The function itself that was getting called, however, needs to be defined elsewhere.Code:var scriptTags = jsDiv.getElementsByTagName('script');//get all script tags if (scriptTags.length != 0){ for(var i=0;i<scriptTags.length;i++){//loop through all script tags and eval() them to run the javascript if(scriptTags[i].innerHTML != "" || scriptTags[i].innerHTML != null){ eval(scriptTags[i].innerHTML); } }
how do you make it run the 'new Function('')' method when the content loads?
Bookmarks