-3

I have following HTML code

<a href="javascript:void(0);" onClick="javascript:OpenNewWindow('/help_email.asp?ProductCode=122030', 350, 250);" class="pricecolor colors_productprice"><b><span class="PageText_L657n"><br><input type="Button" font face="arial" size="2" color="#ffbb00" value="submit best offer"> </Button>

What i want to do is FIND ALL occurrences of help_email.asp and replace it with help_email.php.

user580950
  • 3,558
  • 12
  • 49
  • 94

3 Answers3

3

Use

$('body').html($('body').html().replace('help_email.asp','help_email.php'));
isJustMe
  • 5,452
  • 2
  • 31
  • 47
  • This way if there are any events associated to elements existing on the page before this code runs, or properties (not attributes, as in, stuff added using `prop()` not `attr()`) will be lost because they are not written in the HTML itself – Meligy Feb 10 '12 at 15:28
  • yeah, the question wasn't to specific just want to provide an alternative in case the string was somewhere else than an anchor or link. – isJustMe Feb 10 '12 at 15:36
3

Try

$(function() {
    var anchors = $("a[href*='help_email.asp'], a[onclick*='help_email.asp']");

    anchors.each(function() {
        var anchor = $(this);

        var originalHref = anchor.attr("href");
        anchor.attr("href", originalHref.replace(".asp", ".php") );

        var originalClick = anchor.attr("onclick");
        anchor.attr("onclick", originalClick.replace(".asp", ".php") ); 
    });
});

Note that replace is a standard JS function, not jQuery specific.

References:

Meligy
  • 35,654
  • 11
  • 85
  • 109
0

Assuming that 'help_email.asp' appears only in the href attributes of anchor tags...

$('a').each(function () {
    var href = $(this).attr('href');
    href = href.replace('help_email.asp', 'help_email.php');
    $(this).attr('href', href);
})

but, why not just do this server-side, so the correct urls are already embedded in the page?

pete
  • 24,141
  • 4
  • 37
  • 51