0

Hi i Usually use this code to replace a text in a page

    $(window).load(function() {
var html = document.body.innerHTML;
html = html.replace( /Any Text/g, '???' );
document.body.innerHTML = html;
});

The problem i have is i cant isert a url instead of "Any Text"... my question is how do you do this:

$(window).load(function() {
    var html = document.body.innerHTML;
    html = html.replace( /http://subd.url.com/index.php?page=category/items/392/???/all/-1/-1/%20/%20/%20//g, 'http://www.newurl.com/foro/' );
    document.body.innerHTML = html;
    });

Thanks,

Also i know its better to use an Id like this..

  $(window).load(function() {
    $("#stID").attr("href", "http://www.newurl.com/foro/");
    }); 

But this time i have to look in the entire body thanks.

jquery is running so if anyone know how to do it with jquery yes im open to jquery. thanks

Jules Martinez
  • 682
  • 1
  • 6
  • 23
  • This is what you want good luck! http://stackoverflow.com/questions/37684/how-to-replace-plain-urls-with-links Or in your case: `function replaceInBody(text){var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; html.replace(exp,"???"); }` – Nate-Wilkins Aug 13 '13 at 22:13

2 Answers2

0

Escape special chars with a \ like so..

html = html.replace( /http\:\/\/subd.url.com\/index.php?page=category\/items\/392\/???\/all\/-1\/-1\/%20\/%20\/%20\//g, 'http://www.newurl.com/foro/' );

This should replace all links on a page to the same destination link..

[].map.call(document.getElementsByTagName('a'), function(a){a.href = 'http://mysite.com'})
C B
  • 12,482
  • 5
  • 36
  • 48
  • thanks but even with escaping special chars is not working.. and i just need to redirect 1 url. this url is generated on php, i dont want to compromise the fetching of the urls, thats why i thougt it will be simple to redirect just one url using javascript – Jules Martinez Aug 13 '13 at 22:55
0

Well, I don't think you're being very clear about what you're trying to find and replace, but at face value, it looks like you are wanting to replace an exact url with another url, so you can do this (jQuery):

$(document).ready(function() {
  $('a').each(function() {
    var href = $(this).attr('href');
    if (href=="http://subd.url.com/index.php?page=category/items/392/???/all/-1/-1/%20/%20/%20/")
      $(this).attr("href","http://www.newurl.com/foro/");
  });
});
CrayonViolent
  • 32,111
  • 5
  • 56
  • 79
  • You are correct, yes im trying to redirect just 1 url to another url.. this url is generated in php i dont want to compromise, this fetching and its pretty advanced for me, so i thougt redirecting just 1 url would be easy.. but even with your code, i cant do it.. thanks appreciate. – Jules Martinez Aug 13 '13 at 22:58
  • well if the href of the link is correct, and it's not working, then I'd say the link(s) you are worried about either a) don't exist when the document is first loaded (e.g. - they are in some ajax response, or otherwise dynamically generated after page is loaded), or b) the links don't directly point to the url in question, and some other js is being invoked onclick and eventually forwarding to the target url. – CrayonViolent Aug 13 '13 at 23:16
  • is there any way to look for the url once the page is fully loaded? – Jules Martinez Aug 14 '13 at 00:56
  • well yes..the code I presented is wrapped in `$(document).ready(..)` for that very reason.. – CrayonViolent Aug 14 '13 at 03:37
  • yes i also tried $(window).load(function()... =( bad luck for me – Jules Martinez Aug 14 '13 at 04:28