0

I have this PHP code which I use to populate my HTML:

<?php $xml = simplexml_load_file("txt/main.xml", null, LIBXML_NOCDATA); ?>

And then I use this JS code to get the xml for use with internal vars:

window.xml = <?php echo json_encode($xml); ?>;

When I "view source" in my browser, the entire xml string is displayed like so:

window.xml = {"comment":[{}, etc. etc., LONG STRING OF TEXT FOR ALL SITE XML, etc. etc.]};

I've read read this post already: How to pass variables and data from PHP to JavaScript?

I'd like to continue with json_encode if possible - is there a way to hide the JS variable from the HTML source?

Community
  • 1
  • 1
calipoop
  • 792
  • 9
  • 31
  • Not really, you can fetch it with ajax, but it's still not secure, and trivial for any user to see in the browser. – adeneo Oct 12 '15 at 21:11
  • What are you trying to hide, and why does it need to be hidden? – azium Oct 12 '15 at 21:13
  • using ajax to fetch the php url will keep it out of view-source – dandavis Oct 12 '15 at 21:15
  • I'm not trying to hide anything too sensitive, it just seems very cluttered and a waste of space to see all site content twice (first for the actual HTML, second in the JS var which has all the HTML text as well as extra internal vars) – calipoop Oct 12 '15 at 21:17
  • in that case, $.ajax or $.load can replace php's include. – dandavis Oct 12 '15 at 21:19
  • Yeah, I had been using ajax to load the entire xml file anyway, but when the need arose to use php for html, I thought I could get away with loading the xml only once... – calipoop Oct 12 '15 at 21:19
  • Unfortunately, I need to use php to populate meta tags. – calipoop Oct 12 '15 at 21:19

0 Answers0