开发者

read php output into a js string?

开发者 https://www.devze.com 2022-12-21 20:07 出处:网络
Basically, I have an html page and I want to create a separate javascript file that reads all of the markup between thetags into a javascript string.Is there a way to do that?

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/mypag开发者_开发问答e.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>'; 


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消