2

I need to update href value thorughout the page using jquery. Say href="http://www.google.com?gsec=account" should be changed to href="http://account.google.com?gsec=account" how i can get this done.

Patrick
  • 73
  • 2
  • 2
  • 5

7 Answers7

7

This will do the replace throughout the page that I think you're looking for.

// Find `<a>` elements that contain `www.google.com` in the `href`.
$('a[href*="www.google.com"]').attr('href', function(i,href) {
       // return a version of the href that replaces "www." with "accounts."
    return href.replace('www.', 'accounts.');
});

Try it out: http://jsfiddle.net/dT8j6/


EDIT: This version allows for https:// and for links without the www..

Try it out: http://jsfiddle.net/dT8j6/1/

$('a[href*="google.com"]').attr('href', function(i,href) {
    return href.replace(/^http(s*):\/\/(www\.)*google.com/, 'http$1://accounts.google.com');
});

EDIT: If you only wanted to change elements that have gsec=account, then change the selector to $('a[href*="gsec=account"]').

user113716
  • 318,772
  • 63
  • 451
  • 440
  • Won't this modify links to google that don't have the "gsec=account" param? – jmar777 Aug 11 '10 at 14:44
  • @jmar777 - Yes. I wasn't certain if that specific param was the requirement. Changing the selector to `$('a[href*="gsec=account"]')` would focus only on those elements. – user113716 Aug 11 '10 at 14:47
  • Ya... which is probably 98% safe, but of course fails for yahoo.com?gsec=account, or gsec=accountfoo, etc. Not sure how _exact_ he needs this thing to be. Question is somewhat vague. – jmar777 Aug 11 '10 at 14:52
  • @jmar777 - Yes, I'm assuming OP controls the content of the href attributes. The selector can be as specific as OP needs. Could use `$('a[href$="google.com?gsec=account"]')` which adds `google.com` and uses the `ends-with` selector instead of `contains`. There will always be a way to break it if OP doesn't give specific requirements. :o) – user113716 Aug 11 '10 at 14:58
3

lets say you have this:

<a href="http://www.ibm.com" id="myLink">

you should use this:

 var newHref = "http://google.com";

 $("#myLink").attr('href', newHref );
leora
  • 188,729
  • 360
  • 878
  • 1,366
1

This is a duplicate of:

How to change the href for a hyperlink using jQuery

One scenario it doesn't mention is if you set an id on your anchor that needs to be changed then you can use the id as a selector in jQuery.

$("#LinkId").attr('href', "http://account.google.com?gsec=account")
Community
  • 1
  • 1
Waleed Al-Balooshi
  • 6,318
  • 23
  • 21
1

This should hopefully provide a pretty full solution to your problem (as best I can interpret it, anyway):

$(function() {
  $('a').each(function() {
    var $this = $(this),
      href = $this.attr('href');
    var res = href.match(/(.*?)(www)(\.google\.com.*?)([?&]gsec=)([^&]+)(.*)/);
    if (null != res) {
      // remove the "full match" entry
      res = res.slice(1);
      // replace www match with account match
      res[1] = res[4];
      // update the href attribute
      $this.attr('href', res.join(''))
    }
  });
});


edit: If "account" is a static value, then this will work as well:
$(function() {
  $('a').each(function() {
    var $this = $(this),
      href = $this.attr('href');
    var res = href.match(/(.*?\/\/)(www)(\.google\.com.*?)([?&]gsec=account)(&?.*)/);
    if (null != res) {
      // remove the "full match" entry
      res = res.slice(1);
      // replace www match with account match
      res[1] = 'account';
      // update the href attribute
      $this.attr('href', res.join(''))
    }
  });
});

Please note that these solutions assume that there may be other variations in the URL, such as http/https and other query string variables.

jmar777
  • 38,796
  • 11
  • 66
  • 64
0

I think you left something out in your question. Nevertheless:

var link = $("a"); // Or some other selector to access the link
link.attr("href", "http://account.google.com?gsec=account");
RoToRa
  • 37,635
  • 12
  • 69
  • 105
0

Use jQuery’s .attr() method. With one parameter, it returns the attribute value, with two parameters you set it.

$("a#whatever").attr("href", "http://account.google.com?gsec=account");
scy
  • 7,132
  • 2
  • 27
  • 35
0

If you are changing all the links then:

$(document).ready(function() {
$("a").attr("href", "http://account.google.com?gsec=account");
});
gnome
  • 1,113
  • 2
  • 11
  • 19