SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript on pages called using ajax

    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

  2. #2
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    any help?

  3. #3
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't get you. You wanna make an Ajax request after the page loaded? Use window.onload.

  4. #4
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, i want the javascript that is in the content that was called by ajax to work

  5. #5
    SitePoint Zealot the DtTvB's Avatar
    Join Date
    Jul 2006
    Location
    Thailand
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code:
    <Page>
    
        <HTML><![CDATA[
            Contents should go here. No JavaScript codes should be here.
        ]]></HTML>
    
        <JS><![CDATA[
            // JavaScript code goes here.
        ]]></JS>
    
    </Page>
    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'))()`.

  6. #6
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how do you make it run the 'new Function('')' method when the content loads?

  7. #7
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    813
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Location
    USA
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

    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);
             }
         }
    The function itself that was getting called, however, needs to be defined elsewhere.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •