views:

396

answers:

3

Hi there,

I have an iframe with id "appframe", the source is page.html and it's on the same server. I want to get the source of the page using jQuery.

alert($("#appframe").contents().find("html").html());

returns <head></head><body></body> even though the document does not contain those tags, it only contains "Default page", so that is exactly what it should return. Any idea how to get the right source script of the whole document using jQuery?

Eg. if the document would be "test" then that is exactly what it should return.

Please note: I did ask this before a few weeks ago. Unfortunately, I got no solving answer. Because of my lower reputation (~85), I can not request a bounty... This doesn't make much sense to me. Anyway, it forced me to repost this question.

A: 

This may be irrelevant... but why do you create an Iframe to post your own content? Why not simply post it in the same document?

Makes it a bit easier to retrieve the source code.

In other words - what are you trying to accomplish?

Steven
Irrelevant, yes. It's not an answer to my question. Same thing happend last time, anyway, I do need this and don't think I need to explain why. Thanks.
Tom
It’s not irrelevant per se. You’re the one having a problem; seeing the bigger picture often helps to provide a solution. You don’t need to explain why you *have* to do this; but then don’t complain if people can’t help you.
Martijn
+2  A: 

The call to $("#appframe").contents().find("html").html() retrieves what should be inside the <html></html> tags.

In the case of an IFRAME, if it has not finished loading, it returns the blank <head></head></body></body> that should otherwise be present.

If you make the call inside $(document).ready(), the IFRAME may not be have finished loading. Try making the call from inside a button click event.

Keep in mind that if your IFRAME contains plain text and you try to gets its html(), it will include the aforementioned blank head and body tags even though they don't exist.

If you don't need to access all the HTML in the IFRAME, you should call the ("body").html() to get the contents. Try calling this from inside a button click event:

alert($("#appframe").contents().find("body").html());

Jose Basilio
+1  A: 

Answering a question with a question is helpful in philosophy, not here. I disagree that the op needs to post why he doesn't put the original document in the page.html. I find it obnoxious when a specific question about x is derailed by "Y does a better job!" and "What about Z?" Funk that. Answer the question if you can, that's great, if not, move along. Oh but I need context, I need to have a glass of whine first, blah blah blah. +1 for sticking to the question original poster, I say.

sZX c