You mentioned that the example.com
may change so I suspect that actually you need the base url just to be able to use relative path notation for your scripts. In this particular case there is no need to use scripting - instead add the base tag to your header:
<head>
<base href="http://www.example.com/">
</head>
I usually generate the link via PHP.