JQuery: moving AJAX response in Chrome / Safari

I am trying to execute an AJAX response that contains a remote web page (HTML output).

My goal is to iterate over the 'script', 'link' and 'title' elements on the remote page - load them, if necessary, and paste its contents into the current page.

It works fine in FF / IE, but for some reason Chrome and Safari behave differently: When I run the .each () loop in the response, Chrome / Safari seems to skip everything that is in the page section.

Here is my current code:

$.ajax({
    url: 'remoteFile.php',
    cache: false,
    dataFilter: function(data) { 
        console.log(data); 
        /* The output seems to contain the entire response, including the <head> section - on all browsers, including Chrome/Safari */

        $(data).filter("link, script, title").each(function(i) {
            console.log($(this)); 
            /* IE/FF outputs all of the link/script/title elements, Chrome will output only those that are not in the <head> section */
        });

        console.log($(data)); 
        /* This also outputs the incomplete structure on Chrome/Safari */

        return data;
    },
    success: function(response) {}
});

I struggled with this problem for a long time, I found some other similar cases when searching on Google, but no real solution. This happens in both jQuery 1.4.2 and jQuery 1.3.2.

.indexOf() .substring() - , .

!

+3
1

, , jQuery HTML- DOM. jQuery <div> innerHTML HTML-, $(...), DOM, <div> jQuery.

, , - <html>, <head> <body>, <div>. , , -: , , - .

, - - . .

$(
    // replace <html> with <foohtml> .. etc.
    data.replace(/<(\/?)(head|html|body)(?=\s|>)/g, '<foo$1$2')
).filter("link, script, title").each(function(i) {
    console.log($(this));
    // do your stuff
});

, , filter , . :

$(
    // replace <html> with <foohtml> .. etc.
    data.replace(/<(\/?)(head|html|body)(?=\s|>)/g, '<foo$1$2')
).find('link,script,title').andSelf().filter("link,script,title").each(function(i) {
    console.log($(this));
    // do your stuff
});
+1

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


All Articles