views:

49

answers:

1
+1  A: 

If I understand you correctly, you want to read the content of the HTML tag (<html>...</html>) with JavaScript?

Easy to do:

var str = document.getElementsByTagName('body')[0].innerHTML;

EDIT: Fixed syntax error.

EDIT2: Used body instead of html tag.

Franz
Would having a variable name `string` be a conflict?
Anthony Forloney
I'm not sure how to read the file into that string? There's two files, mypage.tpl.php, which contains the markup and content, and then there's mypage.js, which is meant to read the contents in from the tpl.php file. It looks like that code would be posted directly into the tpl.php file, but I actually need it to live on its own?
phpN00b
@Anthony: I don't know - fixed it just to be safe.
Franz
@phpN00b: Wait a second. When exactly is `mypage.js` supposed to read the contents? On the server or on the client? Is it included in the output of `mypage.tpl.php`?
Franz
Apologies... I'm not sure of all the proper terms to describe what it is I want to do. mypage.tpl.php contains all of the markup for the page itself (actually shows at http://mysite.com/mypage.php). I want a separate script, mypage.js that, onload, will read the contents of mypage.php (everything between the <body></body> tags) and store all of that as a variable. So that, I can do:var string = the markup from the php filedocument.write(string);and have the output be the markup itself...
phpN00b
What are you trying to accomplish exactly phpN00b? There might be an easier way.
Anthony Forloney
You can just eliminate the middle-man (*javascript*) and do all the output'ing in PHP.
Anthony Forloney
No, I need to output it in javascript. I'm trying to get around an iframe issue where the iframe lives on a separate server than the file it's being read into to.
phpN00b
Is the file, `mypage.tpl.php`, on the separate server? Do you have access to it?
Anthony Forloney
Right now, they are on the same server. They soon won't be. It doesn't really matter because I know that I want to read the php file into a javascript variable as described above. I don't want a pure php solution, nor do I want a pure js solution for many reasons. I need it to work exactly as I described.
phpN00b
I updated my code to read the contents of the `body` tag instead of the `html` tag. Try adding `alert(str);` to see if you get the output you want. Can you tell us what you need the content for as at the time it will be read by JavaScript it will already have been printed out by the PHP page.
Franz
The whole goal is to make it accessible to another site that lives on a separate server. Originally, we attempted to use an iframe to do this, but due to cross-site scripting restrictions, that was a no-go. We are trying to read the content of the page into a js script that can be "included" in the <head></head> section of the page on the other server so that they can simply print out all of the markup from that one variable.
phpN00b
Then above code should work for you. The contents of the `body` tag are inside the `str` variable. (You can obviously change it back to `html` if you want to get the `head` stuff, too.
Franz
This doesn't work cross-site. I can't seem to use document.write(str) on a separate server and have it echo the contents of that variable.
phpN00b
Then I suggest you use AJAX features to get the other site's content and then just parse the XML returned. Easy to do with JQuery (as you seem to be using that).
Franz