0

I am trying to do a find and replace throughout an entire page, and add some parameters to any URLs that contain some specified text. (i am speaking about hardcoded inline a href URLs)

So for example, I want to replace any instances of this:

<a href ="http://localhost/wordpress/">Link</a>

With

<a href ="http://localhost/wordpress/?demo_mobile_site">Link</a>

I tried some replace function I found, but I cant get it to work with the forward slashes in the string.

Any help on this would be appreciated. Thanks

levi
  • 23,693
  • 18
  • 59
  • 73
  • To clarify.. you want hard coded links on your page to be modified by javascript if, say, javascript detects that the visitor is on a mobile phone? or some other such variable criteria? Or are you just trying to permanently change the links regardless of external factors? – arkigos Jul 07 '11 at 20:16
  • I am trying to permanently change the links, regardless of any factors. – levi Jul 07 '11 at 20:19
  • I added a link sample in answer below which works fine . – Pit Digger Jul 07 '11 at 20:22

4 Answers4

2

You don't need t replace anything just simple add onto a string.

$('a').each(function(){
    var _href = $(this).attr('href');
    $(this).attr('href', _href + (_href.charAt(_href.length-1) == '/' ?  "? demo_mobile_site" : "/?demo_mobile_site");
});

or if you just want to replace the one href you can do something like this:

$('a[href^="http://localhost/wordpress"]').each(function(){
    var _href = $(this).attr('href');
    $(this).attr('href', (_href.charAt(_href.length-1) == '/' ? _href + "?demo_mobile_site" : "/?demo_mobile_site");
});

This including url's with query strings:

$('a').each(function(){
    var _href = $(this).attr('href');

    if ( _href.indexOf('?') >= 0 ){
        $(this).attr('href', _href + "&demo_mobile_site=");
    } else if ( _href.charAt(_href.length-1) == '/' ) {
        $(this).attr('href', _href + "?demo_mobile_site");
    } else {
        $(this).attr('href', _href + "/?demo_mobile_site");
    }
});
locrizak
  • 12,192
  • 12
  • 60
  • 80
  • The thing Is in some instances I will want to replace parts of the code. For example to turn "http://localhost/wordpress/?page_id=143" into "http://localhost/wordpress/?demo_mobile_site&page_id=143" I am looking for a formula that I can reuse for different instances – levi Jul 07 '11 at 20:28
0

Check this sample I created with replace it works fine.

http://jsfiddle.net/zRaUp/

 var value ="This is some text . http://localhost/wordpress/ Some text after ";
    var source= "http://localhost/wordpress/";
     var dest = "http://localhost/wordpress/?demo_mobile_site";
    alert(value);
     value= value.replace(source,dest);
    alert(value);
Pit Digger
  • 9,618
  • 23
  • 78
  • 122
0

Are these always in links? If so:

$('a[href="http://localhost/wordpress/"]').attr('href', 'http://localhost/wordpress/?demo_mobile_site');
Marc
  • 3,812
  • 8
  • 37
  • 61
0
$('a[href="http://localhost/wordpress/"]').each(function(){
    var newhref = $(this).attr('href') + '?mobile_site';
    $(this).attr('href', newhref);
});
Tak
  • 11,428
  • 5
  • 29
  • 48
  • What if you some reason there was no ending `/` this wouldn't work – locrizak Jul 07 '11 at 20:21
  • Change the [link selector](http://stackoverflow.com/questions/303956/jquery-select-a-which-href-contains-some-string) to `a[href^="http://localhost/wordpress"]` – Tak Jul 07 '11 at 20:23
  • Your link to jsfiddle is incorrect. Can you please add the full URL. Thanks – levi Jul 07 '11 at 20:32