2

I was interested in obfuscating my JS code, but I realized arround forums that it is useless. I would like to obfuscate my code anyway. So I was wondering, is that possible to execute JS code on server side (with an app in node.js for example), and just call via Ajax function with context (like dom, or something else), execute on server side, then give back result to page.

It could be very usefull for me, that could permit to just show basic JS functions, but not core of my app...

Perhaps a solution already exists, but I found nothing on Web...

EDIT :

I thought that with node.js, a solution was existing. I meant for example a simple JS function in client side like : call_func('function_name', context); that call a server side JS dispatcher function with ajax, that returns JS object containing results.

Perhaps I am dreaming ? :)

Thanks for your help.

12
  • 3
    Just move everything into your core language for your website, like PHP. Commented Jul 12, 2012 at 14:52
  • How ? I use for example the dom to calculate my results. So If I execute on server side, I have to know how the dom is, in "real time". I use JQuery too... Commented Jul 12, 2012 at 14:54
  • The server and the client are 2 separate entities. They can only "talk" through http requests and responses (either ajax, page post, etc). If you have proprietary information/business logic in your JS (which you should never do and I would strongly suggest getting someone to code review with you), then move that code to your server side language and only handle basic events on the client side. Commented Jul 12, 2012 at 14:56
  • 1
    Rewrite your calculation script in php and execute it on php side, js will call the php via ajax. that's much easier than trying to run a js through a browser on the server. Commented Jul 12, 2012 at 14:56
  • 2
    You can't have it both ways. JS server side is possible, but you can't manipulate the DOM in the server, that's what client code is for. Commented Jul 12, 2012 at 15:04

3 Answers 3

1

You can either rewrite your calculations in PHP or if you need to use them dynamically/get access to the DOM, you can use AJAX to calculate on the server side using PHP and then recieve the ouput of the PHP script without reloading the page.

You can read about AJAX here (I would recommend using jQuery for it as it is much simpler than trying to understand HTTP requests):

http://api.jquery.com/jQuery.ajax/

Sign up to request clarification or add additional context in comments.

1 Comment

I already use Jquery. I edited my message to better fit my needs and my thoughts.
0

While I agree that you should probably use a serverside language and get the information that you need from the dom via ajax, it is not true that you have to do that. You can simulate a dom on the server using e.g. jsdom which you will find here https://github.com/tmpvar/jsdom for nodejs. Similar packages exist for other languages.

That dispatch thingy you were dreaming about could be achieved using nowjs http://nowjs.com/.

I dont have experience with either tool, so I cannot comment on how well they work.

1 Comment

That seems incridible ! I read that it is synching in real time, does it include DOM ? Can't vote for your answer, but I may I if I could... I'll try tomorow and give you all results. But it could be the very best way to obfuscate code !
-1

AJAX is the way to go. Send function name in xhr.send('func=myfunc').
First create a dummy div.

<div id="dummy"></div>

Then create a switch case in the js code and call the function:

switch(<?echo $_POST['func'];?>){  
  case 'myfunc':  
    document.getElementById("dummy").innerHTML=myfunc();  
    break;  
}

Then just use xhr.responseText.

2 Comments

Nope, cause div will be filled with JS, so it can be read by client :)
you just want to keep your js functions in the remote file secure right?? If that is the case this will do. Only the div is visible which is not an issue..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.