1

Basically, I have an html page and I want to create a separate javascript file that reads all of the markup between the tags into a javascript string. Is there a way to do that?

EDIT

Okay, so, to elaborate a little more...

In my tpl.php file, I basically have these lines js snippet:

<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript">var text = '<div id="open">test text</div>';</script>
<script type="text/javascript" src="/js/mypage.js"></script>

Then, in mypage.js, I have this:

$(document).ready(function() {
    var content = text;
        alert(content);
});

And this accurately displays "test text" in an alert window, but I'm looking for the variable to actually look like var text, so that it reads:

var content = '<div id="open">test text</div>'; 
1
  • please, explain better, what's your goal? you can access DOM in javascript easily, just use jquery or similar library. Commented Mar 2, 2010 at 0:18

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

15 Comments

Would having a variable name string be a conflict?
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?
@Anthony: I don't know - fixed it just to be safe.
@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?
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 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 file document.write(string); and have the output be the markup itself...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.