Passing relative URL variables to include files

I hope I can explain this clearly.

Okay, I have a global javascript function held in a global directory.  Basically it takes a url passed into it when a person clicks on a tab and via Ajax Updater loads that url into a text area.

So I have this tabbed widget which uses this function which needs to be included in lots of different microsites in different directories.


========The widget looks like this:==========
<div id="aNiceWidget">
<li><a href="#" onclick="return populateIBox('parent_microsite_root/_includes/', this);">Stuff1</a></li>
<li><a href="#" onclick="return populateIBox('parent_microsite_root/_includes/', this);">Stuff2</a></li>
<div id="iBoxtext">Some info is coming any day now</div>

* Where parent_microsite_root = the root of the microsite page that has the widget in it.

=========The global function looks like this:==============

   var theObj=null;
   function  populateIBox (serverPage, obj){
        theObj = obj;
       var objID = "iBoxText";
       new Ajax.Updater(objID, serverPage, {method:'get', onComplete:function(){
//load initial page into the widget
Event.observe(window, 'load', function(){ populateIBox('relevant_parent_microsite_root/_includes/', null); });

So my question is this:
Because this is a widget that will sit in lots of different files I cannot put absolute urls in there.  I need it to get the page's parent url and then tag on the rest of the relative path to the end.
I also need the initial load in the javascript to do the same thing.
In other words I need for both the widget and the javascript to recognise the microsite's parent url as a variable.

I have this variable available in each microsite:  <?php $doc_root = $_SERVER['DOCUMENT_ROOT'];  ?>
Can I use that?

Thanks if you can help.
Who is Participating?
IsisagateConnect With a Mentor Commented:
if that is a js include file change the file name to whatever.php and include it like you would a js file...   all you have to do is output through an echo or as I showed the content in that server variable into the javascript and it will do the rest, either directly or via a javascript variable..

var parent_root = '<?= $_SERVER['DOCUMENT_ROOT'];  ?>';

<div id="aNiceWidget">
<li><a href="#" onclick="return populateIBox('<?= $_SERVER['DOCUMENT_ROOT'];  ?>/_includes/', this);">Stuff1</a></li>
<li><a href="#" onclick="return populateIBox('<?= $_SERVER['DOCUMENT_ROOT'];  ?>/_includes/', this);">Stuff2</a></li>
<div id="iBoxtext">Some info is coming any day now</div>
Lady_MAuthor Commented:
Hi Isisagate.
Sorry can you clarify what you want me to do to my global.js file?
You say I need to save it as global.php and include this variable in there:
var parent_root = '<?= $_SERVER['DOCUMENT_ROOT'];  ?>';

If so, won't that variable have the value of the php file's parent, rather than the microsite which is calling it?  

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Lady_MAuthor Commented:
I don't know php by the way.
Lady_MAuthor Commented:
Can i use ajaxpack.basedomain ?
stevenlevithanConnect With a Mentor Commented:
I'm having a hard time following some parts of your question. Are you trying to get the domain name of the page the script was called from? If so, there is the window.location object, which contains various properties about the current page's URL. For example:

window.location.protocol + "//" + window.location.hostname + "/"

...would give you "" if run from this page.

For more information about window.location, see

If you needed to extract parts of URLs other than the current page, you could use my parseUri function (see: ).
Lady_MAuthor Commented:
Okay I'll try to explain.

On the website page there will be a box, it has three tabs at the top and when people click on the tabs they will see three different blocks of text loaded into the box.  Those three blocks are specific to the particular site they are on.  E.g. "This site's copyright info is such and such", "This site is about such and such" and "Our company is about such and such".  

Tthe organisation I'm building this for actually manages about 40 different sites all on the same host server, but with different domains.  They all need this same box so I want to reuse it as an include file.

Each site has exactly the same sub directory structure, the only thing that is different between them all is their domain.  So one site would have these three pages to load into its tabbed box: 

and another would have these three pages to load into its tabbed box:  

So I wanted to make the tabbed box an external object and have it call an external javascript to make it work.  

So in each site, I have the javascript file included in the head.  And in the body I have the included as an external file.

I then need that included tabbed box to pick up the domain of the page it is included *in* (ie, or
rather than its own domain (
or the domain of the javascript (

I tried using a variable in the object, but I'm really not too sure how this works, and whether variables are picking up the page domain, or the object's domain or in fact the javascript's domain.  What I need is the page's domain.  I have the second problem of the javascript loading an initial page, so it also need's the page's domain.

I tried a few of the obvious things with no luck and I'm not sure if it's my syntax which is wrong or whether it's more fundamental.

Could somebody explain to me the process and if possible the correct syntax?

leos_Connect With a Mentor Commented:

Workaround 1: pass a domain variable when you make your ajax call.  I assume you're calling some .jsp page with the method GET - so put a domain name into the querystring, then edit that .jsp page to populate the baseURLs before it sends.

Workaround 2: make your links relative, that is begin them each with / and they'll match your current domain.

However, to do what you want, I'm thinking you have a .js file that's included on every page in you site.  Add this line to it:

  gv_DomainName = '';  //global variable

Now you should be able to access this varibable from inside your widget code, but only if the widget is imported into a page that includes this header.  

I hope this helps.
Lady_MAuthor Commented:
Hi all
thanks for the replies, I haven't forgotten about this issue, I've just had alot of urgent stuff to get done.  i will take a look at this this week and get back to you.
Forced accept.

EE Admin
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.