Hiding/Obfuscating A Javascript Variable In The Source
Let's say, for the sake of argument, I have a page with a progress bar that's advancing based on the number of times a certain hashtag has been tweeted on Twitter. This could be generated something like this:
tweets = <?php echo $tweetsfile->total; ?>;
target = <?php echo $target; ?>;
$('#progressbar').css('width', tweets / (target/100) + '%');
Supposing that it's undesirable for people to be able to look at the source code and see what the target number is. Is there a simp开发者_如何学Pythonle strategy for keeping this information from prying eyes?
instead of doing the calculation client side, do it server side.
$('#progressbar').css('width', <?php echo ($tweetsfile->total / ($target/100)); ?> + '%');
You can simply compute the percentage serverside (php) and not clientside.
$progress = ($tweets / ($target/100));
And output only $progress, which will be the percentage.
$('#progressbar').css('width', $progress + '%');
Try just calculating the percentage in php:
progressPercent = <?php echo (100 * $tweetsfile->total / $target); ?>;
$('#progressbar').css('width', progressPercent + '%');
精彩评论