I’m trying to replace all instances of http:///
on my website with /
using global regex like this:
$('body').html($('body').html().replace(/http:////g,'/'));
It works well, but unfortunately this somehow breaks all the other Javascript-bound events and functions I have on my website.
Does someone know of a way to achieve the same without breaking the other functions?
I tried to find other ways of using global regex but I’m guessing this somehow messes with the DOM, so not sure how to fix this.
Thank you!
2
Answers
Instead of replacing the HTML content directly, you can traverse the DOM and update the specific text nodes that contain the target string:
Chnaging the innerHTML removes all the event listeners bound. So I would just alter the content that has the issues directly. If it is links, select all the links with the problem and replace them attribute.