• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2617
  • Last Modified:

Passing relative URL variables to include files

Hello
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.

So,

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

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

And
=========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/iBox_stuff1.inc', 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.
0
Lady_M
Asked:
Lady_M
3 Solutions
 
IsisagateCommented:

<div id="aNiceWidget">
<ul>
<li><a href="#" onclick="return populateIBox('<?= $_SERVER['DOCUMENT_ROOT'];  ?>/_includes/iBox_stuff1.inc', this);">Stuff1</a></li>
<li><a href="#" onclick="return populateIBox('<?= $_SERVER['DOCUMENT_ROOT'];  ?>/_includes/iBox_stuff2.inc', this);">Stuff2</a></li>
</ul>
<div id="iBoxtext">Some info is coming any day now</div>
</div>
0
 
IsisagateCommented:
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'];  ?>';
0
 
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?  

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Lady_MAuthor Commented:
I don't know php by the way.
0
 
Lady_MAuthor Commented:
Can i use ajaxpack.basedomain ?
0
 
stevenlevithanCommented:
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 "http://www.experts-exchange.com/" if run from this page.

For more information about window.location, see http://developer.mozilla.org/en/docs/DOM:window.location

If you needed to extract parts of URLs other than the current page, you could use my parseUri function (see: http://blog.stevenlevithan.com/archives/parseuri ).
0
 
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:
www.site-one.com/_infofortabbedbox/page1.html 
www.site-one.com/_infofortabbedbox/page2.html 
www.site-one.com/_infofortabbedbox/page3.html 

and another would have these three pages to load into its tabbed box:
www.site-two.com/_infofortabbedbox/page1.html 
www.site-two.com/_infofortabbedbox/page2.html 
 www.site-two.com/_infofortabbedbox/page3.html  

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 tabbedbox.inc 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, www.site-one.com or www.site-two.com).
rather than its own domain (www.host.com/_global/_includes/)
or the domain of the javascript (www.host.com/_global/_scripts/)

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?

Thanks
0
 
leos_Commented:
Hi,

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:

<script>
  gv_DomainName = 'www.site-one.com';  //global variable
</script>

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.
0
 
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.
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now