Forum Moderators: open
I'm using the following method to grab content from another page (in my case, to integrate CMS's)
From the source page (here called sourcepage.html) I have something like:
<ul>
<li><a href="link1.html">link1</a></li>
<li><a href="link2.html">link2</a></li>
</ul>
and so forth...
Then on the destination page I use the following code to pull that list of links and drop it into the page:
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Ajax and Jquery: Remote Pages</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
$('#list').load("sourcepage.html ul"[smilestopper]);
});
</script>
</head>
<body>
<h2>Here is the list from the other page:</h2>
<div id="list"></div>
</body>
</html>
ie from the source page, the AJAX pulls the ul, then drops it into a div called "list"
The problem I am having is that if the destination page is in a sub-directory, when I pull a link from the source page, such as "domain/link1.html"
on the destination page within the sub directory it is written as
"domain/sub_directory/link1.html"
How do I make it so that it pulls the content properly without attempting to put it into the same level directory as the destination page?
I am sorry if this message didn't make sense; I am new to AJAX and javascript and possibly used some incorrect terminology.
I can link to the article where I found this message if that is permitted.
Best Regards,
Daniel
link1.html
link2.html
These are better:
/link1.html
/sub_directory/link2.html
Some folks always include the domain even for internal links (for when scrapers grab their content):
http://www.example.com/link1.html
http://www.example.com/sub_directory/link2.html
That way, there's never any confusion about where a link points. Once you get in that habit, you'll find many linking problems just go away. :)
It the link is "http(s)://example.com/whatever" just use it.
If the link is "/whatever", add the domain name and use the result.
If the link is "./whatever, remove the period, add the domain name, and use the result.
If the link is "whatever", take the URL of the page from which you are grabbing this link, remove everything starting from the end of that URL back to the last slash in that URL, add the link ("whatever") after that last slash, and use the result.
If the link is "../whatever, then take the URL of the page from which you are grabbing this link, remove everything starting at the end of the URL back to the second-to-last slash, add the link value to that, and use the result.
If the link is "../../whatever, then take the URL of the page from which you are grabbing this link, remove everything starting at the end of the URL to the third-to-last slash, add the link value to that, and use the result.
... I trust you can see the pattern here. This describes how canonical, server-relative, and page-relative links are resolved by browsers, and there are only two additional loops involved in coding it -- the slash counter, and the "../" counter.
Jim
<a href="page1.htm">
href = http://www.example.com/path/to/current/page/page1.htm
<a href="/page2.htm">
href = http://www.example.com/page2.htm
<a href="../../page3.htm">
href = http://www.example.com/path/to/page3.htm
The method I outlined above describes the way in which *all* HTTP user-agents must resolve links; There's no wiggle-room to it at all. So if the function you're calling doesn't work that way, then it's broken (assuming that it is the correct function to be using for this application).
Jim
In other words, if I do this:
<html>
<head>
<title>Test</title>
</head>
<body>
<a href="page1.htm">Page 1</a>
<a href="/page2.htm">Page 2</a>
<a href="../page3.htm">Page 3</a><script type="text/javascript">
var aList = document.getElementsByTagName("a");
for (var i = 0; i < aList.length; i++) {
alert(aList[i].href);
}
</script>
</body>
</html>
It will show me the computed value of the href.