Closed Thread Icon

Topic awaiting preservation: relative paths into absolute paths (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8598" title="Pages that link to Topic awaiting preservation: relative paths into absolute paths (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: relative paths into absolute paths <span class="small">(Page 1 of 1)</span>\

 
maninacan
Paranoid (IV) Inmate

From: Seattle, WA, USA
Insane since: Oct 2001

posted posted 04-08-2003 17:57

OK, so on this page I include a page from another site via php's readfile(). And a lot of the images and links are broken because they are relative paths (/images/images.gif instead of http://www.domainname.com/images/images.gif, same deal with links) Would there be a way in JS to change all the relative paths to absolute paths?

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 04-08-2003 18:37

If you are able to insert text into the page this doesn't actually take Javascript
putting this within the head tags:

<base href="http://thedomain.com">

should do the trick. I'd bet this could also be set via js but I don't know the syntax






.:[ Never resist a perfect moment ]:.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 04-08-2003 19:44

You could probably use this JS code, but it wouldn't take effect until the page has loaded:

window.onload = function() {
for (var a=0; a < document.links.length; a++)
{
// for each link...
if (document.links[a].href.substr(0,7) != 'http://') // if it doesn't start with this...
document.links[a].href = 'http://www.domain.com' + document.links[a].href; // prepend it with the domian name
}
}

It won't work right for links that are *purely* relative, like, relative to the current page. That's more difficult.

maninacan
Paranoid (IV) Inmate

From: Seattle, WA, USA
Insane since: Oct 2001

posted posted 04-09-2003 06:13

Hmm, interesting. Is there a way to get what's in the address bar. Because then you could just erase everything on the end until a slash and then append the relative link and then that'd work for those links that are relative to the page.

trib
Paranoid (IV) Inmate

From: Den Haag, Netherlands
Insane since: Sep 2002

posted posted 04-09-2003 09:46

It's early in the morning, I'm at work, hassled and thinking on my feet, so apologies if this is a stupid answer ... but ... while you're still in the PHP part of the process, couldn't you either add the base address to the header (as bitdamaged suggests) ...

or if you are including the file in another one, rather than just using it in isolation ... couldn't you again within PHP ...
read the file into an array ... and then on each element of the array, use preg_replace to change the first slash of any substring beginning with space-slash or equals-slash into the base address of the full url ... then print the array to build your page ... ??

maninacan
Paranoid (IV) Inmate

From: Seattle, WA, USA
Insane since: Oct 2001

posted posted 04-10-2003 06:00

If I knew how to do the base thing with php I would. I'll go look for it now, but I've never heard of it.

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 04-10-2003 18:09

There's not so much a base thing in php

What he's talking about here is something like parsing the doc for links and images and then replacing them with the appropriate absolute paths.



.:[ Never resist a perfect moment ]:.

maninacan
Paranoid (IV) Inmate

From: Seattle, WA, USA
Insane since: Oct 2001

posted posted 04-12-2003 02:38

Hey,
finally got a chance to try out that base thing. Works real well, thanks. Didn't know there was such a thing, pretty cool.

-Jason

« BackwardsOnwards »

Show Forum Drop Down Menu