I have a function that loads parts of other pages into my div #ajaxloadconent, here is the function:
$('#ajaxloadcontent').load(url+' #ajaxloadcontent');
However, the problem is that I am unable to run javascript on the page that I load, while using this function. I simply want to run this:
<script type="text/javascript">
document.title = 'PearlSquirrel About';
</script>
When the data is loaded through .load(). The script is contained on the page that I load, so I do not want to modify the .load() function that I use. If anyone knows how to fix this, then help would be greatly appreciated. I can also provide further information if necessary.
1 Answer 1
Try to use this
$.ajax({
url: url,
dataType: 'html',
success: function(data) {
//Do something with the data e.g.
$(data).find("#ajaxloadedcontent").appendTo("#ajaxloadcontent");
}
});
from the jQuery docs:
"html": Returns HTML as plain text; included script tags are evaluated when inserted in the DOM.
Or you can use http://api.jquery.com/jQuery.getScript if you seperate your script to a standalone js file
#ajaxloadcontentelement?$.ajaxto give you more control over it. simply replace hisalert(data)with$(data).find("#ajaxloadedcontent").appendTo("#ajaxloadcontent"). replace.findwith.filterif#ajaxloadedcontentis a direct child of the body or if it is the only html in the document.