Meta programming into several output languages
The Emscripten project (which I only spotted last week) might interest you: http://syntensity.blogspot.com/2011/04/emscripten-10.html
This guy has basically written a compiler for C/C++ that compiles to Javascript code.
That should solve the Javascript side of your problem.
Hope that helps.
Another product along the same lines, and a bit more well known is Google Web Toolkit (GWT). It's based on Java, but the end result is similar -- you write your web application in Java code and it compiles the front-end parts into Javascript and the back-end parts into regular Java bytecode. I know you're not asking for Java, but if it interest you, the link is here: http://code.google.com/webtoolkit/
Slightly less useful, but possibly more relevant to your question is PHPJS. This is a project to implement as much of the PHP language in Javascript as possible. They're doing it on a function-by-function basis, so it's only ever going to be an approximation, but given that the language syntaxes are similar, it may be possible to use it to write code that works unchanged in native PHP and also in Javascript on the client side.
Of course the one big down-side of compiling one language into another is that the resulting code is always going to be sub-optimal. There's not much you can do about that, but it's worth bearing in mind before you start down the path of writing a shared code-base in a single language.
You could take a look at Haxe. Haxe is an open source programming language. It can be compiled to JavaScript, Flash/ActionScript, PHP, C++, Java, C#, Python and Lua.