Login

Designing Code

An insider's view into the dark world of marketing and how to design code with the right side of your brain.

Javascript Function to Run

 

The crappy thing about html returned from ajax calls is that the returned html doesn't execute any javascript "onload" calls. This function is called on an ajax success AFTER the called html has been placed inside the ajax target div. The function finds any "onload" javascripts inside "script" tags and eval()'s them.  This is necessary for wysiwyg editor elements inside returned html from ajax calls as well as multilayer ajax calls.  

 
 
function core_activate_ajax_scripts(containerObject) 

    childNodeArray = containerObject.childNodes; 
    
    scripts = containerObject.getElementsByTagName("script"); 
    if(scripts.length>0) 
    { 
        for(var n=0;n
        { 
            eval(scripts[n].innerHTML); 
        } 
    } 

 
///////////////////////// 
//example call 
///////////////////////// 
 
function getSelect(value,otherValue) 

    // define url  
    var url = "/modules/Query-GFF/controllers/ajax/get_select.php"; 
    
    var response = new Ajax.Request( url, 
    { 
        method: "post", 
        parameters: 
            { 
            'value': escape(value), 
            'othervalue': otherValue 
        }, 
        onSuccess: function(transport) 
        { 
            var result = transport.responseText; 
            
            //create the target div object 
            target_object = document.getElementById('my_target_div'); 
            
            //place the ajax returned html in the target div 
            target_object.innerHTML = result; 
            
            //execute any javascript elements that require an "onload"" 
            core_activate_ajax_scripts(target_object); 
        } 
    } 
); 
    
}

 

Posted by kcbruce at 2010-11-05 1:41pm | Permanent Link | Comments(0)

Share |

Home  • NeutralGood Blog  • Tech Blog  • Portfolio  • Resume  • Scripts  • Photos  • Contact
Phone: (240) 415-8721
http://www.kevinbruce.com · Site Modified Mar 15, 2013 · ©2013 Kevin Bruce | Designer of Web & Print
Follow me on LinkedIn Profile   Facebook   Twitter