Download jquery with inline javascript

I am using jquery loading to get a div on another page and paste it into my page. something like that:

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

In the div on another page there is javascript in the div. Javascript does not occur, only html content. Is there a way to get everything in a given div?

+6
source share
4 answers

It 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, check out the source code of both pages that make it up:
Demo source code: http://jsfiddle.net/FRbnD/4/
Source code for "another page": http://jsfiddle.net/MWkSj/1/

The idea is to fetch another page using the $.get query and then find #otherpagediv and to clone it . Then you add the clone to #mydiv . If you insert a clone in the DOM, and if this clone contains an SCRIPT element, this SCRIPT will be executed.

+6
source

From the documentation :

Note. When calling .load () using a URL without the suffix selector expression, the content is passed to .html () before the scripts are deleted. This executes script blocks before they are dropped.

If .load () is called with a selector expression appended to the URL, the scripts are deleted before the DOM is updated, so they are never executed.

+2
source

JavaScript should also come up with an answer. You must make sure that /Pages/grid2.aspx should send the required response from the server. Also, the URL you passed to the load method has a space in it. I think you should fix it and try.

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

http://www.coursesweb.net/ajax/execute-javascript-code-ajax-response_t

You may find this page useful, I used a script, it uses eval ()

 // this function create an Array that contains the JS code of every <script> // then apply the eval() to execute the code in every script collected function parseScript(strcode) { var scripts = new Array(); // Array which will store the script code // Strip out tags while(strcode.indexOf("<script") > -1 || strcode.indexOf("</script") > -1) { var s = strcode.indexOf("<script"); var s_e = strcode.indexOf(">", s); var e = strcode.indexOf("</script", s); var e_e = strcode.indexOf(">", e); // Add to scripts array scripts.push(strcode.substring(s_e+1, e)); // Strip from strcode strcode = strcode.substring(0, s) + strcode.substring(e_e+1); } // Loop through every script collected and eval it for(var i=0; i<scripts.length; i++) { try { eval(scripts[i]); } catch(ex) { // do what you want here when a script fails } } } 
0
source

Source: https://habr.com/ru/post/903479/


All Articles