33

I have seen a lot of websites with some function (p,a,c,k,e,d) in their JavaScript code. The different websites may have different bodies of this function, but they all use the same parameter names (p,a,c,k,e,d). Is it a standard or a library or something?

Secondly, it seems that this function is supposed to be executed as soon as the page loads. Like the following snippet from a website.

Can you help me in understanding this code? eval() is used to evaluate expressions like 2+3 but how is the following code passing a function to it?

try{
        eval(
            function(p,a,c,k,e,d)
                {
                  //some code goes here
                }
    }catch(err){}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Naveen
  • 7,944
  • 12
  • 78
  • 165

3 Answers3

29

So if you use http://matthewfl.com/unPacker.html as I posted in the comments, it "unpacks" the code into this:

(function()
    {
    var b="some sample packed code";
    function something(a)
        {
        alert(a)
    }
    something(b)
}
)();

It doesn't seem to be malicious. For a soft argument on why you would use this, see javascript packer versus minifier:

Packed is smaller but is slower.

And even harder to debug.

Most of the well known frameworks and plugins are only minified.


Packer does more then just rename vars and arguments, it actually maps the source code using Base62 which then must be rebuilt on the client side via eval() in order to be usable.

Side stepping the eval() is evil issues here, this can also create a large amount of overhead on the client during page load when you start packing larger JS libraries, like jQuery. This why only doing minify on your production JS is recommend, since if you have enough code to need to do packing or minify, you have enough code to make eval() choke the client during page load.


Minifier only removes unnecessary things like white space characters where as a Packer goes one step further and does whatever it can do to minimize the size of javascript. For example it renames variables to smaller names.
  • @InsaneCoder So essentially packing is less efficient but seems to do actual compression compared to minifying. Also see [Quick question: Packed or minified?](https://groups.google.com/forum/#!topic/swfobject/rULPCQTL-K4) –  Jan 29 '14 at 06:26
  • 1
    that's packer not packed - packed code does not uncompress that way. just fyi. - I have not yet found out which compressor uses "p,a,c,k,e,d"... – Toby Dec 17 '14 at 23:56
4

It's a function which decompresses compressed/obfuscated javascript code. Many JS libraries and scripts make use of it.

There are online tools where you can pack and unpack code via the browser, which use the function.

eren
  • 708
  • 8
  • 20
flauntster
  • 2,008
  • 13
  • 20
  • Can you please add to your answer why we want to compress javascript code, to reduce size or to make it complex to increase its security? – Naveen Jan 29 '14 at 06:17
  • Can be used for either or both of those reasons really, smaller script files means faster loading of a page's assets, and some developers like to protect their code via obfuscation. – flauntster Jan 29 '14 at 06:20
  • @InsaneCoder Compressing JS code will remove the unused variables, removes comments, console.logs(which is evil in IE browsers),reduced memory size(renders faster in browser). hope you got it – Praveen Jan 29 '14 at 06:20
2

As I have seen that eval(function(p,a,c,k,e,d){}) is used in http://www.indiabix.com which uses it for hiding whole contents when user get download the page and open it . Maybe that is the inner workings of the particular code.

Anthony
  • 15,435
  • 4
  • 39
  • 69