Get innerhtml from external page with Javascript

前端 未结 6 567
离开以前
离开以前 2021-02-04 17:47

I\'m trying to get innerHTML of a DIV that is located on external page. It is possible to do this using this kind javascript code?

    

        
6条回答
  •  一向
    一向 (楼主)
    2021-02-04 17:53

    not quite. Only if the desireed page is on the same domain and same protocol you could try to embed it in an iframe or you could use ajax. jQuery already has implemented a way to retrieve only a part of an external page ,via ajax:
    $.get(url+"#"+id); , where id is the div's id. so you would have something like :

    var aux = window.location.href.split('/'),
    id = 'glr1',
    page = 'my_page.html';
    $.get(
      aux.splice(0,aux.length-1).join('/') + '/' + page + '#' + id,
      function(){alert(arguments[0]);}
    );
    

提交回复
热议问题