thesunneversets
thesunneversets

Reputation: 2580

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 simple strategy for keeping this information from prying eyes?

Upvotes: 0

Views: 155

Answers (3)

Jonathan M
Jonathan M

Reputation: 17451

Try just calculating the percentage in php:

progressPercent = <?php echo (100 * $tweetsfile->total / $target); ?>;
$('#progressbar').css('width', progressPercent + '%');

Upvotes: 0

helloandre
helloandre

Reputation: 10721

instead of doing the calculation client side, do it server side.

$('#progressbar').css('width', <?php echo ($tweetsfile->total / ($target/100)); ?> + '%');

Upvotes: 2

Paul S.
Paul S.

Reputation: 1537

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 + '%');

Upvotes: 1

Related Questions