Forcing .src to the "right" server


Csaba Gabor

The situation is simple, but I can't figure out a compact
summary so please bear with me:

I have a page wherein the src of a <script src="jsLibSomeName.js">
MUST be run through my apache server (because it gets
redirected to a .php file which is really spitting out the .js).

There are, therefore, 3 situations that I see:
1. Invoking the page externally
2. Invoking the page via localhost or similar
3. Invoking the page by double clicking it

If the invoking is from 1 or 2, apache sees that the jsLib*
..js file is being sought and does a (302) redirect
and this is working nifty. The line below is an example
from my httpd.conf (apache configuration file) of how the
localhost version works.

RewriteRule (^|/)jslib/?([^/]+).js$ http://localhost/jsLib.php?name=$2 [NC,L]

In scenario 3, however, IE or FF is looking for file:///....
It's not going to the server! Simple, you say, just
have the src be like src=""
The problem with that, though, is that if I am (A) disconnected
from the internet or (B) don't have a domain name active
(because maybe I don't have a static IP just now) or (C)
DNS servers are out of kilter cause maybe that dynamic IP
just changed, then I'm stuck waiting forever in 2 and 3

So it seems that I can't set a fixed src to force being
passed efficiently through my Apache web server in all situations.

So my question (drumroll, please, what everyone's been
waiting for) is: What is the cleanest way to set this up
so I can develop locally, but have availability globally
in a seamless way? Seamless to me, that is - I don't
want to think about this construct once I've set it up.

Thanks for any ideas,
Csaba Gabor from Vienna

The following idea works with IE, but I can't use Firefox
(see bug
Immediately change the href on the <BASE href=...> if
I am on localhost or file:///

Here's how it looks:
<script type='text/javascript'>
// next statement changes the BASE href in FF and IE, but only IE takes note
if (location.protocol=="file:")
document.getElementsByTagName('BASE')[0].href =
<script type='text/javascript' src='jsLibOut.js'></script>


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question