This question is different from those asked. I have a page named page_1 which only loads information from page_2 into page_1 when a link is clicked. But it only loads the information within a specific div without reloading the page. Sometimes that information contains a JavaScript that must be executed but with jquery load, the information within the div is loaded but the JavaScript is not executed.
Page_1.php
<a href="page_2.php" rel="charger_partiel" data-direction="id_du_milieu" data-ajax_id_to_update="id_du_milieu_2" data-afficher_espace_de_chargement="Oui" data-id_image_de_chargement_devant_lien="devant_charger_partiel">charger_partiel <span id='devant_charger_partiel'></span> </a><br><br>
<div id="id_du_milieu_2">Le text affiche par defaut</div>
<script>
$(document).on('click', 'a[rel*=charger_partiel]', function(f) {
f.preventDefault();
var myId_to_update_here = $(this).data('ajax_id_to_update');
var myId_to_load_from_other_page = $(this).data('direction');
var afficher_chargement = $(this).data('afficher_espace_de_chargement');
var id_chargement_devant_lien_partiel= $(this).data('id_image_de_chargement_devant_lien');
var pageURL=$(this).attr('href');
history.pushState(null, '', pageURL);
var image_de_chargement = '<img src="images/facebook_style_loader.gif" />';
var preloader_comme_facebook_et_youtube = image_de_chargement ;
if(id_chargement_devant_lien_partiel!='')
{
$('#'+id_chargement_devant_lien_partiel).html(image_de_chargement);
}
if(afficher_chargement=='Oui')
{
$('#'+myId_to_update_here).html(preloader_comme_facebook_et_youtube);
}
$('#'+myId_to_update_here).load($(this).attr('href')+' #'+myId_to_load_from_other_page,
function(responseText, textStatus, XMLHttpRequest) {
if(textStatus == 'error') {
$('#'+myId_to_update_here).html('<p>Oupps.. There was an error making the AJAX request. Maybe your internet connection is slowing down.</p>');
}
});
//Apres le chargement, on enleve le loading
if(id_chargement_devant_lien_partiel!='')
{
$('#'+id_chargement_devant_lien_partiel).html('');
}
//Fin de l'affichage du chargement si l'id du span du devant lien est mis
return false;
});
</script>
Page_2.php
<h3 id="id_du_milieu">
Anything from here should be loaded
<div id="id_de_test">id_de_test</div>
<div id="id_de_gloire">id_de_gloire </div>
<script> alert('This is a test that should be executed'); </script>
And it ends here
</h3>
<h1>This should not be loaded</h1>
<script> alert('This also should not be executed'); </script>
The page loads infos from the other page with <h3 id="id_du_milieu">
, however it doesn't execute the javascript alert.
How to execute only the script that is between the selected id from the other page which in this case is id="id_du_milieu"
?