Jquery select and update head node in ajax $ .get call

I want to do a very easy task to replace the head of a node page with the head of a node page called by ajax call.

I do it like this:

        url = 'http://myurl';                    
        $.get(url, function(results){
          var head = $('head', results);

          $('head', results).each (
             function() {
                 alert("got a match ");
             });


          var form = $("form.ajax_form_result", results);
          //update the ajax_form_result div with the return value "form"
          $('head').html(head);
          $('#ajax_form_result').html(form);
        }, "html");

Form updates work without problems, but chapter updates do not work. $ ("head", results); does not return node, which leads to an empty head in a new document.

+3
source share
3 answers

try: $("head", $(results));

"results" should only be a block of text in html format. "$ (result)" should be that block converted to DOM objects.

+1
source

This worked for me:

var feed = http://www.feed.com/page.html;

jQuery.get('http://yourdomain.com/proxy.php?url='+feed+'', function(html){
    if (html == '') {
        alert('Invalid URL');
        return false;
    } 

    html = html.replace( /<(\/?)(html|head|body)([^>]*)>/ig, function(a,b,c,d){
        return '<' + b + 'div' + ( b ? '' : ' data-element="' + c + '"' ) + d + '>';
    });

    RSSlink = jQuery(html).find('[data-element=head]').find('link[type="application/rss+xml"]').eq(0).attr('href');

});

He should be able to do what you need!

0
source

:

var myUrl="http://urlExampleSubstituteWithYour"
$.get( url, function( data ) {
    // Get the string index of the content of the start head tag
    var startHead=(data.toString().indexOf("<head>"))+6;
    // Get the string index of the finish head tag
    var stopHead=data.toString().indexOf("</head>");
    // Create a string of the head content of the myUrl page
    var head=data.substring(startHead+6,stopHead);
    // Apply HTML to my block...
    $("#idOfMyBlock").html(head);
});

( , ) ! !

0
source

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


All Articles