Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery load with inline javascript

I am using jquery load to get a div on a different page and insert it into my page. somthing like this:

$('#mydiv').load("/Pages/grid2.aspx" + " #otherpagediv");

In the div on the other page, there is javascript in the div. The javascript is not coming across, only the html content. Is there a way to get everything in the specified div?

like image 618
user957863 Avatar asked Dec 09 '11 20:12

user957863


1 Answers

This works:

$.get( '/Pages/grid2.aspx', function ( data ) {
    $( '#mydiv' ).html( $( '<div></div>' ).html( data ).find( '#otherpagediv' ).clone() );
});

Live demo: http://jsfiddle.net/FRbnD/4/show/light/

To understand the demo, view the source code of both pages that comprise it:
Source code of the demo: http://jsfiddle.net/FRbnD/4/
Source code of the "other page": http://jsfiddle.net/MWkSj/1/

The idea is to retrieve the other page via a $.get request, and then find the #otherpagediv and clone it. You then append the clone to the #mydiv. If you insert a clone to the DOM and if that clone contains a SCRIPT element, that script will be executed.

like image 113
Šime Vidas Avatar answered Sep 22 '22 00:09

Šime Vidas