I'd like to pass some data from PHP to JavaScript without JSON.
The reason is because I don't want the data been readable by anyone if clicks on view page source.So, I have a PHP like
print(<script type="text/javascript">a = "aaa";</script>);
In my HTML code this will be
<script type="text/javascript">a = "aaa";</script>
I can remove this in the client side, after loading the variable. By for example with jquery
$('script[type="text/javascript"]'开发者_StackOverflow中文版).remove();
And after the DOM will not have anymore the script tag, but the variable a.
Later if I type to the console window.a
will be aaa
.
But i do not want to show the <script type="text/javascript">a = "aaa";</script>
in my HTML source code. Is this possible, to pass the PHP variable directly to the DOM?
Thanks for the help.
JavaScript is a client-side language. Whatever you pass to it (by whatever means) will be readable by the end user.
Removing the Script DOM won't help, as "view source" shows the HTML code as it was during download. If that is what you are concerned about, you can fetch the variable via an AJAX once the DOM has been loaded.
(But it still is readable by anyone who can read JavaScript (an re-run the AJAX call), use Firebug or Wireshark. It really only helps against a simple "view source".)
精彩评论