Link to home
Start Free TrialLog in
Avatar of brianmfalls
brianmfallsFlag for United States of America

asked on

Using the javaScript replace() method in IE.

I have a form that has draggable elements and droppable areas.  When a thumbnail sized draggable element is placed within its associate droppable area, the script replaces the thumbnail image with a html comment, and it changes the primary image's display attribute from none to block.  The replace '_t' is necessarily redundant for a case which is not being addressed here.

Processing is managed @ two points w/in the script:
1)   initial processing
var primaryContent = "#".replace('#', ui.draggable.html().replace('_t','').replace('display: none;','display: block;'));

Open in new window

2)   this is the simplist of three conditional statements
var primaryContent = primaryContent.replace(/img.*>/, '!--image removed-->');

Open in new window



It seems as if IE ignores/fails to replace anything.  The included code is my debug output [console.log('');] from both Firefox and Internet Explorer.

Have any of you dealt with anything like this in the past?  Is IE not compatible with the replace() method?  (I am testing in version 8)
From Console (Firefox)
<!--image removed-->
<div style="display: block; margin: 0px; padding: 0px;">
	<span style="font-family: Arial;">
		<img contentelementid="32623" src="http://my.qdobamail.com/userfiles/34/e3d71ab3-3795-102c-9fa9-4fc2de14c7ac/images/mainImages_600x400/6961-football-650x400.jpg" alt="" height="400" width="650">
		<br>
	</span>
</div>

From Console (Internet Explorer)
<IMG style="WIDTH: 50px; HEIGHT: 31px; OVERFLOW: hidden" class=contentElement alt=32623 src="/userfiles/34/3038/32623.jpg"> 
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: none; PADDING-TOP: 0px"><SPAN style="FONT-FAMILY: Arial"><IMG alt="There's more to explore TM at&#13;&#10;BEAVERS football games.&#13;&#10;Qdoba now has three locations at Reser Stadium. The Club Level,&#13;&#10;The North Side, and our new location in the South End Zone Corridor.[icon]" src="http://my.qdobamail.com/userfiles/34/e3d71ab3-3795-102c-9fa9-4fc2de14c7ac/images/mainImages_600x400/6961-football-650x400.jpg" width=650 height=400 contentElementID="32623"><BR></SPAN></DIV>

Open in new window

Avatar of GeoffHarper
GeoffHarper
Flag of United States of America image

You might try putting /findme/g around all the search strings.  "g" is for global and it's a Regular Expression syntax to replace all occurences of "findme"
Try this:
var primaryContent = primaryContent.replace(/img[^>]*>/g, '!--image removed-->');

Open in new window


But please note that this will replace as many images as there are in the content, not just the first one.
Avatar of brianmfalls

ASKER

Sorry guys, not ignoring this...  There is an error in IE that is keeping me from getting to where this issue was noted that has to be corrected before I can verify anything.  I'll get to it as soon as I am able.  Thanks so much for your input, and again, I apologize for the delay.
Unfortunately, adding global to the search expression did not change the final render.
add "gi" instead of just "g"
Because IE convert the tags to uppercase, you should also add case-isensitivity flag:
var primaryContent = primaryContent.replace(/img[^>]*>/gi, '!--image removed-->');

Open in new window

Alright.  Lets backtrack a bit.  Global isn't an option.  The first instance of the image tag needs to be replaced with a comment.

var primaryContent = primaryContent.replace(/img.*>/, '!--image removed-->');

Open in new window


So <img attribute="value" attribute="value" /> is changed to <!--image removed-->  The above code works in Firefox, Chrome, and Safari.  It fails in Internet Explorer.  Why is it failing, and what do I do to correct it?
ASKER CERTIFIED SOLUTION
Avatar of imantas
imantas
Flag of Lithuania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
how about trying the below:


var pattern = new RegExp("<img([^<>+]*)>", "gi");
primaryContent = primaryContent.replace(pattern, '<!--image removed-->');

Open in new window

It's the 'i'...  I don't know what I did that it didn't work the first time I tried it, but it worked well enough the second time!  Thanks everybody.