简体   繁体   中英

Scripts inside HTML page via Ajax

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.

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)

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 .

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

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

but would be executed this way:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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