tags:

views:

55

answers:

3
 $(function(){
            $('a').each(function(){
                        var x=this.href;
                        this.href="www.somesitename.com/filter"+this.href;
                  });
         });

i wrote the above jQuery script to append some site name to all the links in the page but it's not working as expected.

+5  A: 

You probably need to add http://:

$(function(){
    $('a').each(function() {
        $(this).attr('href', 'http://www.somesitename.com/filter' + this.href);
    });
});
Darin Dimitrov
This is the correct answer.
Dean
A: 

You will probably need to use the append() function jQuery has http://api.jquery.com/append/

Ankur
The question is about appending to an **attribute** not an **element**
David Dorward
yeah i realised the mistake it was supposed to be attribute.
Bunny Rabbit
+3  A: 

I wonder why would you use jQuery for such basic behaviour...

that is why the tag BASE is there for

Specify a default URL and a default target for all links on a page:

<head>
    <base href="http://www.w3schools.com/images/" />
    <base target="_blank" />
</head>

<body>
    <img src="stickman.gif" />
    <a href="http://www.w3schools.com"&gt;W3Schools&lt;/a&gt;
</body>

don't complicate what is simple!


added

from Sitepoint

The base element is only going to be useful to you if all your relative links or form submissions go to the same location.

balexandre
I can't believe I never knew that...
Dean
that might do the trick i wasn't aware of the base tag.
Bunny Rabbit
Is there a way i can do the same to form action element?
Bunny Rabbit
@Bunny Rabbit Yes
balexandre
but then i also want to extract the default base url which was set before writing the base tag .
Bunny Rabbit
$("head > base").attr("href") ??
balexandre