Tips and tricks

Tackling AJAX and script tags inside

So recently I had to make ajax requests that pull in a piece of page with some scripting inside. The context does not matter much, what matters is the script executed fine on my localhost but not as a WordPress installation in a WP blog. After some head banging turns out that in my localhost I was using jQuery 1.9.1 and WP uses 1.8.3 .

So jQuery 1.9.1 seems to find the script tags too in the pulled ajax content. But what interests me is the WP plugin functionality. I could deregister the 1.8.3 and register the 1.9.1 but that is a very very bad practice. So I prefer a dirty approach better then a bad practice that could damage others.

I wrap my script in a invisible div like so


Then in the javascript just do

jQuery('.toexecute').each(function(){
    var _t = jQuery(this);
    if(_t.hasClass('executed')==false){
        eval(_t.text());
        _t.addClass('executed');
    }
})

at the end of the document and at the end of each complete ajax request.

{"type":"main_options","images_arr":"'#ffffff'","enable_ajax":"off","soundcloud_apikey":"","bg_isparallax":"off","bg_slideshow_time":"0","bg_transition":"slidedown","site_url":"https:\/\/digitalzoomstudio.net","theme_url":"https:\/\/digitalzoomstudio.net\/wp-content\/themes\/qucreative\/","blur_ammount":"26","width_column":"50","width_section_bg":"","width_gap":"30","border_width":"0","border_color":"#ffffff","translate_cancel_comment":"Cancel reply","translate_leave_a_comment":"Leave a comment","translate_leave_a_comment_to":"Leave a comment to","is_customize_preview":"off","width_blur_margin":"30","gallery_w_thumbs_autoplay_videos":"off","content_enviroment_opacity":"30","menu_enviroment_opacity":"70","base_url":"https:\/\/digitalzoomstudio.net"}
{"type":"darkfull"}