Solved

Using the javaScript replace() method in IE.

Posted on 2011-03-22
10
340 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
ID: 35193960
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
ID: 35194379
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
ID: 35317095
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

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

Expert Comment

by:GeoffHarper
ID: 35318527
add "gi" instead of just "g"
0
 
LVL 8

Expert Comment

by:imantas
ID: 35318560
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
ID: 35324965
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
ID: 35325282
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
ID: 35325418
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
ID: 35326342
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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'…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

821 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