html body.find,javascript - Find body tag in an ajax HTML response - Stack Overflow

I experimented a little, and have identified the cause to a point, so pending a real answer which I would be interested in, here is a hack to help understand the issue

$.get('/',function(d){

// replace the `HTML` tags with `NOTHTML` tags

// and the `BODY` tags with `NOTBODY` tags

d = d.replace(/(/gi,'$1NOTHTML$2>',d)

d = d.replace(/(/gi,'$1NOTBODY$2>',d)

// select the `notbody` tag and log for testing

console.log($(d).find('notbody').html())

})

Edit: further experimentation

It seems it is possible if you load the content into an iframe, then you can access the frame content through some dom object hierarchy...

// get a page using AJAX

$.get('/',function(d){

// create a temporary `iframe`, make it hidden, and attach to the DOM

var frame = $('').appendTo('body')

// check that the frame has loaded content

$(frame).load(function(){

// grab the HTML from the body, using the raw DOM node (frame[0])

// and more specifically, it's `contentDocument` property

var html = $('body',frame[0].contentDocument).html()

// check the HTML

console.log(html)

// remove the temporary iframe

$("#frame").remove()

})

})

Edit: more research

It seems that contentDocument is the standards compliant way to get hold of the window.document element of an iFrame, but of course IE don't really care for standards, so this is how to get a reference to the iFrame's window.document.body object in a cross platform way...

var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;

var iframeBody = iframeDoc.body;

// or for extra caution, to support even more obsolete browsers

// var iframeBody = iframeDoc.getElementsByTagName("body")[0]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值