tags:

views:

37

answers:

2

I'm wondering if it's possible to take all <a> tags inside a div and have them open in a new window which uses a subdomain. Here's what I'm working with:

<div id="workshops">
<a href="/_product_119808/Test_Workshop">Test Workshop</a>
</div>

I would like to have the Test Workshop link open up in a new window but also use a subdomain. So the link in the new window would be:

http://workshops.mydomain.com/_product_119808/Test_Workshop

Is this possible?? Thanks in advance!

+1  A: 

Try this:

$(function ()
{
    var domainRoot = "http://workshops.mydomain.com";

    $("#workshops a").each(function ()
    {
        $(this).attr("href", domainRoot + $(this).attr("href"));
        $(this).attr("target", "_blank");
    });
});

It would probably be helpful for other people viewing this question if you explained why you needed to use jQuery for this, though, instead of just outputting the links correctly in the first place.

Domenic
You might want to cache the jQuery object so you don't create it 3 times.
Thomas
Domenic, I need to use jquery because the CMS I'm using only outputs the url as a relative url. It will always default to the main domain and I need to to use a subdomain to enable a different layout for the shopping cart. A hack for sure but the only way I can use a different shopping cart layout in the CMS.
mmsa
Thomas, jQuery does that caching for you.
Domenic
+1  A: 

try this

$( '#workshops a' ).each(function(index,item) {
    var $item = $(item);

    $(item)
    .attr('href', "http://workshops.mydomain.com" + $item.attr('href') )
    .attr('target', '_blank');
});

I would recommend that you set target="blank" via html instead of javascript.

Jared