Using document.body.innerHTML.replace

Hello, I have a small problem with the way document.innerHTML.replace is interpreted..

Put this in the browser window and press enter..


Ideally, only "Javascript" should be replaced by "JS". But the images and formatting are gone. Why ?
LVL 37
Harisha M GEngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hmmm... where on your page are you executing that line of code and what do you mean by:

"images and formatting are gone"

Harisha M GEngineerAuthor Commented:
jessegivy, thanks for the input.

I am not executing that from any page. Just open this question in any browser and put that line in the address bar and press enter. JavaScript will be replaced by JS but the images will not be shown
The only thing I notice is that when you request any of the images url, it has replaced the original server path, with the javascript, ie: javascript:document.body.innerHTML=document.body.innerHTML.replace(/JavaScript/g,'JS');/m.gif

No explanation to why this is happening though.

Does this also happen when you attach this code to a inline button?

Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Harisha M GEngineerAuthor Commented:
> it has replaced the original server path, with the javascript, ie:
I don't understand. It not only happens for that, but also if I give


This way, there is no change in the URL.

>> Does this also happen when you attach this code to a inline button?
I am just testing that code on EE page. I am not developing any web page
I can get a slightly better result by using

But the head section is still missing, very curious.
Well, because you input that text into your address bar, it sees that as a change of address somehow.
No matter what you place at the dots in javascript:document.body.innerHTML=document.body.innerHTML.replace(.......), it will replace all the ' with the javascript 'path' you enter.

Just right-click on one of the missing images and request its properties to see the changed url/path for that image.

Harisha M GEngineerAuthor Commented:
mshogren, yes.. it's an interesting problem :)
Harisha M GEngineerAuthor Commented:
Roonaan, well that's quite right. But why do the images get shown with mshogren's code ?
Harisha M GEngineerAuthor Commented:
Hmm.. I think if I get the source of whole page, I can solve this problem.

Is there any property to get the source of the whole page including the <HEAD> section ?
This will get you even closer, though I have no idea how useful this will be.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Harisha M GEngineerAuthor Commented:
Well done :)

I will be testing it a bit.. and will be back ASAP
Harisha M GEngineerAuthor Commented:
Thanks to all of you.

Here is the code I was trying to create...

javascript: function x() { for(i=0;i<document.anchors.length;i++) { document.anchors[i].parentNode.innerHTML = document.anchors[i].parentNode.innerHTML.replace(/<a name\="(\d+)">/,'<b>http:#$1</b> <a name\="$1">'); } }; x();

Put this onto the addressbar the browser (Firefox) in any question and get the comment id's.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.