Solved

Using the javaScript replace() method in IE.

Posted on 2011-03-22
10
334 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:brianmfalls
  • 4
  • 3
  • 3
10 Comments
 
LVL 4

Expert Comment

by:GeoffHarper
Comment Utility
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"
0
 
LVL 8

Expert Comment

by:imantas
Comment Utility
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.
0
 
LVL 1

Author Comment

by:brianmfalls
Comment Utility
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.
0
 
LVL 1

Author Comment

by:brianmfalls
Comment Utility
Unfortunately, adding global to the search expression did not change the final render.
0
 
LVL 4

Expert Comment

by:GeoffHarper
Comment Utility
add "gi" instead of just "g"
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 8

Expert Comment

by:imantas
Comment Utility
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

0
 
LVL 1

Author Comment

by:brianmfalls
Comment Utility
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?
0
 
LVL 8

Accepted Solution

by:
imantas earned 500 total points
Comment Utility
As I've told previously, Internet Explorer converts all the tags to UPPERCASE. img is converted IMG and because your regullar expression is case-sensitive, it doesn't match your img tags. You should use case-isensitive search as I have also mentioned previously.

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

Open in new window


Please note the letter "i" added after your regullar expression. Also try to combine it with global flag in case it doesn't work:

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

Open in new window


Now two letters ("g" and "i") are added which forces global and case-isensitive search.

Does that help?
0
 
LVL 4

Expert Comment

by:GeoffHarper
Comment Utility
how about trying the below:


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

Open in new window

0
 
LVL 1

Author Closing Comment

by:brianmfalls
Comment Utility
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.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now