Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use javascript to convert relative href attributes into absolute paths?

I have a template that gets screenscraped from an outside vendor and need to include absolute paths in the navigation so the externally hosted content will properly link back to our site.

Right now the page/template is driven by a global menu app written by our back end development staff... so anyone who updates our site goes in and changes the menus and their paths...

Right now all of the links are linking to relative paths back to the root.

For example

<a href="/">Home</a>
<a href="/news/">News</a>
<a href="/media/">Media</a>
<a href="/other/">Other</a>

I need a simple way (preferably with jquery) to prepend "http://www.domain.com" to each of those links.

like image 540
jhensley Avatar asked Dec 31 '22 00:12

jhensley


1 Answers

Please note that jQuery object $("a").attr("href") is not equal to $("a").get(0).href ?

$("a").each(function() {
   alert(this.href);
   $(this).attr("href") = this.href;
});

In you case, this may not help you , because you want static markup, javascript generate dynamic content. But it seems that you want static markup in that case it has to be emit by server.

like image 94
Fred Yang Avatar answered Jan 08 '23 02:01

Fred Yang