简体   繁体   English

使用内联javascript加载jquery

[英]jquery load with inline javascript

I am using jquery load to get a div on a different page and insert it into my page. 我正在使用jquery load来获取不同页面上的div并将其插入到我的页面中。 somthing like this: 这样的事:

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

In the div on the other page, there is javascript in the div. 在另一页的div中,div中有javascript。 The javascript is not coming across, only the html content. javascript没有出现,只有html内容。 Is there a way to get everything in the specified div? 有没有办法在指定的div中获取所有内容?

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/ 现场演示: 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/ 演示源代码: http//jsfiddle.net/FRbnD/4/
Source code of the "other page": http://jsfiddle.net/MWkSj/1/ “其他页面”的源代码: 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 . 想法是通过$.get请求检索其他页面,然后找到#otherpagediv克隆它 You then append the clone to the #mydiv . 然后将克隆附加到#mydiv If you insert a clone to the DOM and if that clone contains a SCRIPT element, that script will be executed. 如果将克隆插入DOM,并且该克隆包含SCRIPT元素,则将执行该脚本。

From documentation : 来自文档

Note: When calling .load() using a URL without a suffixed selector expression, the content is passed to .html() prior to scripts being removed. 注意:使用不带后缀选择器表达式的URL调用.load()时,在删除脚本之前将内容传递给.html()。 This executes the script blocks before they are discarded. 这将在丢弃之前执行脚本块。

If .load() is however called with a selector expression appended to the URL, the scripts are stripped out prior to the DOM being updated, which is why they are never executed. 但是,如果使用附加到URL 的选择器表达式调用.load(),则在更新DOM之前会删除脚本,这就是它们永远不会被执行的原因。

JavaScript should also come along the response. JavaScript也应该出现在响应中。 You have to make sure that /Pages/grid2.aspx should send the required response from server side. 您必须确保/Pages/grid2.aspx应从服务器端发送所需的响应。 Also the url which you have passed to load method has a space in it. 您传递给load方法的url也有一个空格。 I think you should correct that and try it. 我认为你应该纠正这个并尝试一下。

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

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

You might find this page helpful, I have used the script, it uses eval() 您可能会发现此页面有用,我使用了脚本,它使用了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's 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
  }
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM