doubt in including js file in php file
i have two php files where one is executed through url and that file makes ajax calls to second php file . some ajax results return HTML contents as result , which have some javascript events . the events didnt work , so i included the js file whenever ajax calls are made to return HTML contents with JS events . I dont think this is the right way . eventhough i use 'live' events on the ajax returned html contents
but what is the right way
function show_regfunc_to_box(this_id){
var enc_item_id = this_id;
enc_item_id = enc_item_id.split("--")[1];
regfunc_to_item_id_hidden = '#regfunc_to_item_id_hidden--'+enc_item_id;
item_id = $(regfunc_to_item_id_hidden).val();
var current_action_regfunc_to_id = '#current_a开发者_高级运维ction_regfunc_to_id--'+enc_item_id;
if(($(current_action_regfunc_to_id).css('display'))=='none'){
$.getJSON('./ajax_funcs.php?func=regfunc_to_box',{item_id_regfunc_to:item_id,enc_item_id:enc_item_id},function(data){
$('.current_action_regfunc_to_and_chfunc').hide();
$(current_action_regfunc_to_id).center_box();
$.post('./ajax_funcs.php?func=regfunc_to_box',{enble_js_file:true});
$(current_action_regfunc_to_id).html(data.regfunc_box_html);
//$(current_action_regfunc_to_id).center_box();
$(current_action_regfunc_to_id).show();
});
}
//cls_i_item_options();
}
elseif($_REQUEST['func']=='regfunc_to_box'){
require_once('./js/jq_funcs.js');
ajax_fcs::regfunc_to_box($_REQUEST['item_id_regfunc_to'],$_REQUEST['enc_item_id']);
}
Including JS with Ajax returned HTML is really not the best approach. Instead, include javascript files within your root file (your first file), and make sure the second file only returns HTML.
From here on, it's only a matter of Javascript. Events attacted to HTML you got from Ajax request won't work, because the browser doesn't know there are any event attached to that part of DOM. So to make that work, rebind all events within Javascript callback function (that is, a function that executes when Ajax request has been completed).
An example if you're using jQuery:
$.get('second_page.php', function(data){
// that is callback function, let's say
// you are getting a div with id="test"
// as returned HTML
$('body').append($(data));
$('#test').bind('some_event', function(e){
// and that is event callback
});
});
精彩评论