tags:

views:

219

answers:

5

Hello. This is probably a stupid question, but I'm using jquery.load() and Jquery UI's tabs.. the pages that i load in there have detail links which are relative inside them like

<a href="mypage.html">link</a>

So say my main page is myserver.com/rootdir/DefaultTabs.asp So if the page I am loading is in myserver.com/rootdir/dir1/page1.html then when I load it into the tab it is a broken link that looks like myserver.com/rootdir/page1.html

So how do I, for example replace all the links on the page to show the right directory, but NOT replace the link if it is already an absolute link. (for example at the bottom of every page I have myserver.com/rootdir/prefs/EditTab.asp which I still want to work)

Thank you so much. I'm barely venturing into jquery and very behind. I've been struggling with this a few hours and have been trying startswith, search within divs/classes, etc.

I know I'm missing the concept of jquery chaining/finding, but...

+1  A: 

I have run into this problem and the only way I've found to make things work is to always make links absolute... hope someone else has a better way.

larson4
not the only way though
ilya.devyatovsky
A: 

Not sure if this is something that might be useful, but have you considered a dynamic javascript file?

This way you can use PHP and get the $_SERVER variables...

<?php header('Content-Type: application/javascript'); ?>

Place the above at the top of a file called "dynamic-javascript.js.php". Go ahead and add your jquery code underneath this, echoing into PHP when you need to for the PATH's.

To run/include the file, just add it to the top of your HTML document as you normally would with a .js file.

Shot in the dark I know... but it has got me out of some pickles when dealing with URL's and PATHS in javascript files.

Good luck!

Tom

Tisch
I'm already doing this, and I anticipate to use this to handle the dir1 vs dir2 type problem.ie tab1 is in dir1, tab2 is in dir2. Tabs.asp is the "outer page" in rootdir. Therefore all the "file1.asp" type relative links try to load in rootdir instead of rootdir/dir1 or rootdir/dir2thansk for your responses.
+5  A: 

Perhaps the answer is to add <base href="http://myserver.com/rootdir/dir1"&gt; to the <head> of the documents you are loading into the tabs.

I expect that you can do this on the browser side using JQuery, or something similar. But if not, it should be a small change to the server side JSPs (or whatever) that render the pages.

Stephen C
I was excited about this but this does not work. (It works if the page is run standalone, but if the page is loaded with jquery inside the div of another page then it does not work. (ie rootdir/tabs.asp loads rootdir/dir1/tab1.asp which has a link to detailpage1.asp in dir1)
The <base> would need to be in the outermost <head> for the DOM as the browser sees it. I should state that I don't use JQuery. But the <base> element in a <head> is the standard HTML way to relocate relative href URLs in a document.
Stephen C
+1  A: 

Something similar to this should help. It uses regular expressions, which you can craft to precisely meet your needs. I didn't test this, but the regular expression is meant to detect cases where the url does not begin with an absolute path indicator.

$('a', context).foreach(function() { 
    var link = $(this);
    var linkText = $(this).attr('href');
    if (null == linkText.match(/^([\\\/])|([^\\\/]*\:)/)) {
        // It's not an absolute url, so fix it.
        linkText = '\\root\\path\\here\\' + linkText;
        link.attr('href', linkText);
    }
});
John Fisher
+2  A: 

Make your links absolute:

<a href="/mypage.html">link</a>

Instead of:

<a href="mypage.html">link</a>
Rich Bradshaw