Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to run javascript on server side

I have an application which runs on mobile devices such as android, and I have some third party traffic tracking js code to track the usage of my application. So everytime a user opens up my application the js code sends a ping to the 3rd party tracking server. However I'm not sure if there's an easy way to embed the js code directly into my application that's not browser based and couldn't interpret js code. So I may have to execute the js code on the server side. The 3rd party code is like this :

<script type="text/javascript" src="http://www.3rdparty.com/tracking.lib.js"></script>
<script type="text/javascript">
   var instance = new Tracking();
   instance.run();
</script>

What's the easiest way to run this code on server side?

Thanks in advance.

like image 566
Shawn Avatar asked Oct 29 '10 13:10

Shawn


People also ask

Can JavaScript work on server-side?

js is an open source JavaScript runtime environment that lets developers run JavaScript code on the server. If that's too complex for you to understand then you should think of it this way: Node. js is JavaScript that runs outside the browser — on the server.

What is the best way to run JavaScript?

To execute JavaScript in a browser you have two options — either put it inside a script element anywhere inside an HTML document, or put it inside an external JavaScript file (with a . js extension) and then reference that file inside the HTML document using an empty script element with a src attribute.


2 Answers

There are several solutions for running server side javascript (Node.JS, or something based on Rhino), but that code is probably dependent on functionality that only exists in a browser.

EDIT

I answered the question, but I didn't really address the problem. Even if the code will execute run on the server, it won't work as intended. You really need to come up with a better solution for tracking usage of a non web based application.

The question you should have asked is: How can I track usage of an android application?

If you are already running a server, they you could write a simple web service that logs usage, you just need to open a HTTP connection from within your app.

I would be surprised if there aren't existing solutions, but I'm not an Android guy, so I wouldn't know.

I would suggest deleting this question and replacing it with one targeting what you are actually trying to accomplish.

like image 178
mikerobi Avatar answered Sep 19 '22 00:09

mikerobi


I'm not sure if it is really possible.

If it is a Java-Server you can use the BeanScripting Framework, but:

If the Javascript code does some calculation like Screen-Size, Browser etc. You won't get that information.

You can't get the IP nor the HTTP-Client (Which Browser etc.).

I Don't know the JavaScript code, so I may be wrong.

In fact, the Javascript code will call your Server to store the information. You can reverse engeneer the infromation sent, by using a Network-Sniffer.

like image 35
Christian Kuetbach Avatar answered Sep 19 '22 00:09

Christian Kuetbach