Mike Muller
Mike Muller

Reputation: 2297

Taking a relative url and forwarding it to a subdomain using jquery

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!

Upvotes: 0

Views: 290

Answers (2)

Jared
Jared

Reputation: 8610

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.

Upvotes: 1

Domenic
Domenic

Reputation: 112837

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.

Upvotes: 1

Related Questions