Question

I need a little help with this...I'm not too familiar with jQuery (still learning), and so not sure if this is even possible.

I've basically loaded a section of a page using $("#my-div").load("http://www.demo.com #container")

The contents of #container load just find in #my-div. The problem is, the links loaded are relative and therefor invalid.

Is there as way to alter the content loaded such that the url can be changed from something like this:

<a href="/this/sample-link-to-page">sample link</a>

to

<a href="http://mysite.com/this/sample-link-to-page">sample link</a>

Thank you.

Was it helpful?

Solution

You should try something like:

$("#my-div a").each( function(index){
    var link = $(this).attr('href');
    link = 'http://mysite.com' + link;
    $(this).attr('href', link);
});

You get all the links, then iterate over changing them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top