Encode And Pass HTML From PHP To Javascript
i need to pass an html template to a javascript variable in php i tried differents things like json_encode(), str_replace() addcslashes() but javascript always throw an error of un
Solution 1:
I always use a <script>
tag to hold my HTML templates. It's a habit I learned from using Handlebars JS. http://handlebarsjs.com/
The trick is the type="text/html"
attribute, the browser doesn't know what do with it so it doesn't display it and doesn't try to run it as code.
<script type="text/html" id="Template1">
<p>This is a template</p>
<p>More template stuff</p>
</script>
To access the template you can do something like
JQuery:
$('#Template1').html()
Javascript:
document.getElementById('Template1').innerHTML;
Post a Comment for "Encode And Pass HTML From PHP To Javascript"