简体   繁体   English

HTML 页面内的脚本通过 Ajax

[英]Scripts inside HTML page via Ajax

I have this situation:我有这种情况:

I pass a variable via url to an HTML page.我通过 url 将变量传递到 HTML 页面。 The page grabs it via javascript and load via ajax a portion of HTML stored in a file.该页面通过 javascript 抓取它并通过 ajax 加载存储在文件中的 HTML 的一部分。 The first html page (the one who read the variable via url) has some scripts in it.第一个 html 页面(通过 url 读取变量的页面)中有一些脚本。

How can i get them working?我怎样才能让他们工作? I tried with:我试过:

$.get('/it_IT/eni_nel_mondo/'+page, function(data){
    $('body').prepend(data);
});

It reads the content and it seems also the scripts, but it doesn't execute them.它读取内容,它似乎也是脚本,但它不执行它们。

I pasted the entire code here: http://jsbin.com/uceper (it doesn't display anything, so get the source)我在这里粘贴了整个代码: http://jsbin.com/uceper (它不显示任何内容,所以获取源代码)

The script existing in your HTML fragment (or HTML portion as you mentioned) won't be executed, maybe because there is no entry point for it.您的HTML 片段(或您提到的 HTML 部分)中存在的脚本将不会被执行,可能是因为它没有入口点。 I mean for ajax-loaded scripts, DOMReady won't fire.我的意思是对于加载 ajax 的脚本,DOMReady 不会触发。 I suggest using self-invoked functions .我建议使用自调用函数

Update: this function won't be executed when loaded via ajax:更新:当通过 ajax 加载时,不会执行此 function:

function getTime()
{
 // Getting the time;
}

but would be executed this way:但会以这种方式执行:

(function getTime()
{
 // Getting the time;
})();

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

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