通过Ajax在HTML页面中编写脚本

时间:2022-12-06 09:49:17

I have this situation:

我有这种情况:

I pass a variable via url to an HTML page. The page grabs it via javascript and load via ajax a portion of HTML stored in a file. The first html page (the one who read the variable via url) has some scripts in it.

我通过url将变量传递给HTML页面。该页面通过javascript抓取它并通过ajax加载存储在文件中的HTML的一部分。第一个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(它没有显示任何内容,所以获取源代码)

1 个解决方案

#1


1  

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. I mean for ajax-loaded scripts, DOMReady won't fire. I suggest using self-invoked functions.

您的HTML片段(或您提到的HTML部分)中存在的脚本将不会被执行,可能是因为它没有入口点。我的意思是对于加载ajax的脚本,DOMReady不会触发。我建议使用自调用函数。

Update: this function won't be executed when loaded via ajax:

更新:通过ajax加载时不会执行此函数:

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

but would be executed this way:

但会以这种方式执行:

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

#1


1  

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. I mean for ajax-loaded scripts, DOMReady won't fire. I suggest using self-invoked functions.

您的HTML片段(或您提到的HTML部分)中存在的脚本将不会被执行,可能是因为它没有入口点。我的意思是对于加载ajax的脚本,DOMReady不会触发。我建议使用自调用函数。

Update: this function won't be executed when loaded via ajax:

更新:通过ajax加载时不会执行此函数:

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

but would be executed this way:

但会以这种方式执行:

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