Skip to content Skip to sidebar Skip to footer

Find And Replace String

Is it possible to look at a page's source code, find a certain part and replace it with something else before the page loads? I would like to accomplish this using JavaScript so th

Solution 1:

<script type="text/javascript">
function replaceScript() {
    var toReplace = 'http://google.com';
    var replaceWith ='http://yahoo.com';
    document.body.innerHTML = document.body.innerHTML.replace(toReplace, replaceWith);
}
</script>

Then initialise in the body tag to do on page load.

<body onload="replaceScript();">

Should work fine and replace all instances in html body code.

If it is in an iframe with id "external_iframe" then you would modify document.body.innerHTML to be:

window.frames['external_iframe'].document.body.innerHTML

Although I'm not convinced you can use it for an external site.

Seems to be some info here: Javascript Iframe innerHTML


Post a Comment for "Find And Replace String"