I just invented PSEUDO Query!

var objects = $.pquery([
    '##pquery##', // Initialize as PSEUDO Query Object
    [document.body, 'css', [{'background-color':'#f00'}], 'append',
        ['##pquery##', // Helps distinguish array needing parsing from array of arguments
        '<div />', 'text', 'LOL!']
    ['textarea', 'css', ['background-color', '#0f0']]

...Is equivalent to:

var objects = [
    $(document.body).css({background-color: '#f00'}).append($('<div />').text('LOL!')),
    $('textarea').css('background-color', '#0f0')

Example image:

enter image description here

The point?

The point of this is in no way for added readability, but rather to provide a means to execute jQuery via PHP outputted JSON through Ajax. I believe this is going to make possible a nicely structured, and well defined output class for PHP, with very little code.

Ultimately, my goal is to have all clients long polling the server, to accept new Javascript and commands at any given moment. I plan to use jQuery for exactly what it is, as a framework, and extend any common commands/routines directly to it as extensions. Eventually I'll revise the structure to support $.method() calls, without having to have a selector. I might need some ideas for the best way to go about that, without causing problems.

What are some concerns with this?

I wanted to avoid it, but I think I'm going to have to eval the Ajax array, for setting callback functions. Or I'll just pass it to a new <script /> element, and delete it after parse. Another option is to add some method to detect function structures, and pass the quoted code through new Function. The last option I can think of, is to only allow predefined functions, which I've already extended to jQuery. I've heard it is bad to pass Javascript through through JSON, however I feel it is necessary if I'm going to be constructing new objects with bound events. I also feel that maybe this whole thing may be a little redundant if I'm going to just eval the code, but there's still the factor of easy output with PHP, which makes me want to continue with this extension. This is the area that I could really use the most advice and opinions about.

Any structural/general ideas or concerns about this? Or possibly some suggestions to just scrap the whole thing, and start with a different approach to the madness?


I'm not sure if there is a real use for this. Web apps normally don't need to execute arbitrary JavaScript, but instead just have a static library which gets data via AJAX on what they want to do. Can you give a more concrete use case?

Also I don't really understand what you are saying in the "concerns" section. Are you asking how to execute the "generated" JavaScript? In that case I'd use apply and/or call.

Finally I'm not really fond of the marker string ('##pquery##'). I'm sure it's possible to use a different data structure that doesn't require it.

  • \$\begingroup\$ I've added the code, and revised some of my explanations a little bit. For my concerns section, I'm basically trying to decide if I should allow raw Javascript to be passed or not. If you have some ideas for an easily identifiable structure, that would not require the header (##pquery##), please share. I'm mainly trying to get feedback about the structure of the "protocol". Feel free to revise your answer, I'm constantly rereading for things I may have missed. \$\endgroup\$ – Shea Feb 21 '12 at 22:03
  • \$\begingroup\$ Basically, I need a structure that is capable of behaving exactly like jQuery, with full functionality. \$\endgroup\$ – Shea Feb 21 '12 at 22:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.