473,395 Members | 1,584 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Text - Image Replace

Hi members,

I was searching for a specific script and found it here.
But the problem is that it works in Firefox but doesn`t work in Internet Explorer.

Who has the solution to get this script working in IE?

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function MyHead() {
  3. var list = new Array();
  4. list[0] = "blah1^<img src='MyPicture1.jpg'>";
  5. list[1] = "blah2^<img src='MyPicture2.jpg'>";
  6. list[2] = "blah3^<img src='MyPicture3.jpg'>";
  7. var j, k, find, item, repl;
  8. var page = document.body.innerHTML;
  9. for (var i=0; i<list.length; i++) {
  10. item = list[i].split("^");
  11. find = item[0];
  12. repl = item[1];
  13. while (page.indexOf(find) >= 0) {
  14. var j = page.indexOf(find);
  15. var k = find.length;
  16. page = page.substr(0,j) + repl + page.substr(j+k);
  17. }
  18. }
  19. document.body.innerHTML = page;
  20. }
  21. </script>
  22.  
  23. <body onload="MyHead()">
Oct 13 '07 #1
2 2097
phvfl
173 Expert 100+
Hi Martin,

Welcome to TSDN...

I experienced this problem not too long ago. The issue is that you are trying to change the innerHTML of an XML document, I assume that you are using XHTML not HTML.

IE does not allow the innerHTML of a valid document to be changed as if the changed content is not valid XML then the document structure could become corrupted. To add/delete content you would need to create an element and then add it to the document using the appendChild method, for example:
Expand|Select|Wrap|Line Numbers
  1. <body>
  2. <div id="container">
  3. <img id="image1" src="someimage.jpg" alt="Pretty picture" />
  4. </div>
  5. </body>
  6.  
To remove the image:
Expand|Select|Wrap|Line Numbers
  1. var $img = document.getElementById('image1');
  2. var $container;
  3. $container = document.getElementById('container');
  4. $container.removeChild($img);
  5.  
To add some text in a paragraph tag:
Expand|Select|Wrap|Line Numbers
  1. var $para = document.createElement('p');
  2. var $container;
  3. $container = document.getElementById('container');
  4.  
  5. $para.innerHTML="Lorem Ipsum";
  6. // Give the element an id to make it easy to select and remove later.
  7. $para.id="p1";
  8. $container.appendChild($para);
  9.  
This will add the paragraph element at the end of the div. You can use the insertBefore to specify the position of the node.

Another method that may be of use would be the replaceChild method, using the initial XHTML snippet this would replace the image with the paragraph:
Expand|Select|Wrap|Line Numbers
  1. var $img = document.getElementById('image1');
  2. var $container;
  3. $container = document.getElementById('container');
  4.  
  5. var $para = document.createElement('p');
  6. $para.innerHTML="Lorem Ipsum";
  7. // Give the element an id to make it easy to select and remove later.
  8. $para.id="p1";
  9.  
  10. $container.replaceChild($para, $img);
  11.  
Oct 13 '07 #2
Hi Martin,

Welcome to TSDN...
Thnx for your fast reply.

I`ll give this a try
Oct 14 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: juglesh | last post by:
Hello, I need to look through the text on a page and replace certain words with an image or other word something like: read document find all instances of the word "blah" change all...
1
by: Andrew Poulos | last post by:
Say I have a page, which has been created by a third party, and the page may contain some pre-specified text. How can I find and replace that text dynamically? For example, if the page I have...
5
by: SeanT | last post by:
Greetings and salutations! I am having a real issue with the VB.NET TreeView control. I need to display an image (a.k.a. logo, picture, etc) from a file as the treenode object. I can not seem to...
12
by: Charlie King | last post by:
As I understand it, the use of FIR* to replace heading tags with images in visually enabled CSS browsers is now frowned upon on the basis that some screen readers will *nor* read back text that is...
3
by: Alex | last post by:
Hello. First, with AJAX I will get a remote web page into a string. Thus, a string will contain HTML tags and such. I will need to extract text from one <span> for which I know the ID the inner...
5
by: avlee | last post by:
Hello i have a code: /* some code creating picture */ header("Content-type: image/jpeg"); imagepng($new); it draws my picture correctly. But what to do when i want to draw a picture on my...
4
by: vunet.us | last post by:
Please, help me with regular expression to grab image source in any text string using the code below. I only need the regex to be plugged in: var strText = "some text <img src='image.jpg'and...
1
by: helraizer1 | last post by:
Hi all, I have a dynamic image that picks out data from a dynamically created .line file. showimage.php(5) <?php include("linesfile.php5"); $linesDataFile = new DataFile("data.line");
0
by: dreams | last post by:
hi :) i am trying to replace a text with image in the gridview.. the data source for my gridview is a sql table, i want to replace specific row from it with image depending on its original...
6
by: bradyounie | last post by:
I'm writing a program that displays a user-supplied Bitmap and then writes text fields to it. These "text fields" are things that the user can move around on the image, but to render them...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.