Ingulit
Ingulit

Reputation: 1141

Javascript: What does var%20C mean compared to a regular var?

I am trying to interpret someone else's code and am very new to Javascript, and I cannot for the life of me find a Javascript tutorial that explains what this means:

var%20C = 'ABC...', a=123, b=456, ...;

What is var%20C, and how is it different from a regular var?

EDIT: The code is from a bookmarklet.

Upvotes: 2

Views: 567

Answers (2)

Johannes Fahrenkrug
Johannes Fahrenkrug

Reputation: 44808

As already pointed out, the code is url encoded.

You can url decode it and turn it back to "normal" like so:

decodeURIComponent("var%20C = 'ABC...', a=123, b=456;");

If you are 100% sure that the string you are evaluating is trusted and safe (read: if you wrote it) you can run it like this:

eval(decodeURIComponent("var%20C = 'ABC...', a=123, b=456;"));

After that, the variables C, a and b will be initialized.

Please keep in mind though that eval is dangerous and considered "evil" when you can't be 100% sure that the string you are evaluating is trusted and safe.

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1075049

It means the code has gotten mangled. As Kevin B said, it looks like it's been partially URL-encoded, which is not a good thing (%20 in URL encoding = a space).

That should read:

var C = 'ABC...', a=123, b=456/*, ...*/;

If you asked a JavaScript engine to interpret var%20C = ..., it would complain:

SyntaxError: Unexpected token %

Re your comment:

The code is from a bookmarklet if that explains anything.

That explains everything! The code will be decoded prior to being run, so the %20 will turn back into a space before the JavaScript engine sees it.

Upvotes: 4

Related Questions