Add domain to relative URLS inside iFrame

I have a small problem which I am hoping someone may be able to help with. The page setup is a bit weird but it does serve a purpose.

I have an Iframe which lists a bunch of articles with links in the headings. I am simply trying to add the domain to each URL. BTW the Iframe is sitting on the same domain.

The code I have works however it grabs the first link, adds the domain and copies the same URL to each link on the page. It must be stuck in some sort of loop.

Code:

jQuery('#iframe').contents().find("h3.title a").attr("href", "http://www.mydomain.com" + jQuery('#iframe').contents().find("h3.title a").attr("href") );

HTML:

<h3 class="title"><a href="/article1.html">Article 1</a></h3>
<p>Article Text</p>   

I would like to end up with this for each article:

<h3 class="title"><a href="http://www.mydomain.com/article1.html">Article 1</a></h3>
<p>Article Text</p>

<h3 class="title"><a href="http://www.mydomain.com/article2.html">Article 2</a></h3>
<p>Article Text</p>

etc...

I'm not much of a jQuery guru, so I can't seem to figure it out. I'm sure it has something to do with it looping back to the start and requires an each or this declaration somewhere...

Please let me know if you need more info.

Cheers Palle

Answers


Ok...

I seem to have figured this out. Bit of a fluke I must admit.

jQuery('#iframe').contents().find("h3.title a").each(function(){this.href = this.href});

I hope this helps anyone with similar problems.


Need Your Help

Efficiently searching large list of strings

string algorithm search

I have a large list of strings which needs to be searched by the user of an iPhone/Android app. The strings are sorted alphabetically, but that isn't actually all that useful, since a string shoul...

How to make this generic?

java generics

I have got a question. How to make this whole code generic? So the variables, methods and classes? If somebody can show how this have to be done. It would help me a lot!

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.