How to remove linebreaks in IE with Javascript? - javascript

I am trying to remove linebreaks with JavaScript. It works in all browsers except Internet Explorer (tested in ie7, ie8). I have read a lot of posts but couldn't find a solution to the problem.
Var someText = "Here’s your text.\n It has line breaks that need to be removed.\rUsing Javascript.\r\n";
someText = someText.replace(/(\r\n|\n|\r)/gm,"");
What else can I try?
* EDITED *****
Here's more code and a better explanation of my problem:
function checkLength(fn) {
var fn = document.getElementById(fn);
var fnb = fn.value;
var fnb = fnb.replace(/(\r\n|\n|\r)/gm,"");
var len = fnb.length;
...
What I am trying to do is calculate the number of chars in a textarea. I had to calculate with both Javascript and PHP and because of the linebreaks, PHP and Javascript never came to the same number. When removing the linebreaks, it is all good except in Internet Explorer (when I calculate it with Javascript). fnb.replace doesn't change anything in Internet Explorer for the character count so that is why I am sure it does not remove the linebreaks correctly. In all other browsers it is fine, I can see the difference in the counter after removing the linebreaks in javascript. Only in ie it doesn't change a thing. I have tried a couple of things as well as your suggestions below and the char count before removing the linebreaks and after is the same in ie.
///////// MY ANSWER /////////////////////////////////////
function checkLength(fn) {
var fn = document.getElementById(fn);
var fnb = fn.value;
var fnb = fnb.replace(/(\r\n|\n|\r)/gm,"");
var len = fnb.length;
...
Like Tomalak said, my logic could be improved - Sorry for being new to JavaScript & programming. Maybe Tomalak doesn't make any mistakes but I'm sure everyone else does. We have to make mistakes to learn.
Internet Explorer didn't like
var fn = document.getElementById(fn);
var fnb = fn.value;
I had to change it to:
var fnb = document.getElementById(fn).value;
Even if it wasn't logical, it should have worked. It did work in all browsers except ie. It's a bug.
That was the answer I was looking for.

Try this:
someText = someText.replace(/[\r\n]+/gm,"");

In fact, the regular expression works just fine in at least IE8.0.
The real problem is that you wrote Var instead of var; Firefox lets you get away with it, but IE doesn't.

You don't need the m flag, but you ought to replace with a space instead of nothing.
someText = someText.replace(/[\r\n]+/g,' ');

Related

string.match() evaluates to null in IE 8 only

I have some working JavaScript code which runs perfectly in other browsers but don't work with IE 8. It's actually simple piece of code and I really can't figure out what's the problem?
In short, while part never gets executed in IE (d.match(pattern) is always null), in all other browsers I'm getting correct offset.
var ids = new Array(),
d = o.innerHTML, // gets correct HTML code in all browsers
pattern = /id="subblock_(\d+)"/,
p;
while (d.match(pattern) != null) {
// IE never gets here!
p = d.search(pattern);
ids[ids.length] = d.match(pattern)[1];
d = d.substr (p+14);
}
Value of d variable looks like this
<div id="subblock_0">...</div>
<div id="subblock_7">...</div>
<div id="subblock_59">...</div>
Not sure, it looks quite correct to me but obviously Microsoft again doesn't agree with me.
Note: I have tried with IE 8 and last updates of Firefox, Chrome and Opera!
I am too lazy to start up a VM, but if I remember right IE8 does not return quotes. A simple console.log(d) would verify that.
pattern = /id="?subblock_(\d+)"?/,

Error in Firefox while replace string using regexp in JavaScript

try{
var hdnPassenger = $("#ctl00_ContentPlaceHolder1_hdnPassenger").val();
var newTr = $("#hdnCtl").html();
newTr = newTr.replace(/_ID/g, hdnPassenger);
}
catch(ex){
alert(ex);
}
Above code is working fine in the internet explorer, but displayed the following error in the mozilla firefox
InternalError: regular expression too complex
Having done some research into this problem, there are two possible reasons for this error:
The actual regex too complex (not in your case, as you only have /_ID/)
The length of the string you're trying to do the substitution on (I don't know what it is, but probably quite long). It seems that there's some hard-coded limit in some versions of firefox, but I can't vouch for that.
I suggest you do two this: add the values of your hdnPassenger and newTr variables - and at the same time google firefox regular expression too complex - there are plenty of hits.

This code doesn't work in IE

Hi so I have this jQuery/JS script. That basically takes an encoded URL string and parses it and saves variable values into variables.
Basically what PHP's $_GET does.
function getUrlVars()
{
var map = {};
var parts = window.location.search.replace(/[?&]+([^=&]+)(=[^&]*)?/gi,
function(m,key,value)
{ map[key] = (value === undefined) ? true : value.substring(1); });
return map;
}
Basically this script does what I want. From this URL string:
/autopop.html?Email=test%40test.com&LastName=Test+last&&FirstName=+Test+First
I get the values:
Email = test%40test.com
LastName = Test+last
FirstName = +Test+First
What I want to do is Auto-populate a form on this same page with this information.
(I know what you're thinking a server-side script would be a better solution but the boss says we don't have access to that, trust me, I've tried)
Long story short, here's the rest of my code:
var keys = getUrlVars();
$(document).ready(function(){
var fname = keys['FirstName'].replace(/\+/g , " ").trim();
var lname = keys['LastName'].replace(/\+/g , " ").trim();
var email = decodeURIComponent(keys['Contact0Email'].replace(/\+/g , " ")).trim();
$("#Email").val(email);
$("#FirstName").val(fname);
$("#LastName").val(lname);
});
This code gets the job done. All except for one browser. IE.
IE doesn't support decodeURIComponent or so I've read. In any case, I tried using other functions like decodeURI and escape all producing unwanted results.
My google searches have yielded nothing but, semi-interesting articles (totally off-topic but thought I'd just share that).
No solutions. Can anyone shed some light? How do I make this work on IE?
You have read wrong, decodeURIComponent works just fine in IE, even in IE6.
However, trim doesn't work in IE browsers prior to IE 9 and that's what causing your script to crash.
For working alternative, see the accepted answer here: .trim() in JavaScript not working in IE
Or use the jQuery trim() method as you're already using jQuery anyway.

Equivalent of "parentNode" in internet explorer

I wrote some code that modifies the images on a webpage. Works with firefox and safari. But tryingto get it to work with Internet explorer has got me stumped. What is the equivalent of "parentNode" in explorer? Or how does one trick it into working?
images = document.getElementsByTagName('img')
parms = {};
for (a=0;a < images.length;a++){
parent = images[a].parentNode; // <-- What to substitute for explorer?
parms[a] = {};
parms[a].bigsrc=parent.getAttribute("href");
parms[a].w_o = images[a].width;
parms[a].h_o = images[a].height;
parms[a].IsBig = false;
parms[a].loaded = false;
images[a].border=0;
parent.setAttribute("href","javascript:MakeBig('"+a+"')");
}
The problem is with the assignment of the parentNode to a var called "parent." This seems to be a reserved word in IE that breaks the code. Change the var name and it should work.
parentNode works fine in IE (except in certain cases, very likely irrelevant here). The error is almost certainly elsewhere in your code.
Are you expecting the parentNode to be an anchor? It looks like you're trying to just wrap the image in a link. If that's correct, what might work as an alternative is adding an onclick to the image itself, and setting a hand cursor. That could create the appearance of the image being a link without you having to care what the parentNode is.

Javascript regex not working in IE

So I've got this table being generated and each cell is given a unique id which is r#c# where the # is the row/column. I've got the code below that extracts the row number and column number from the ID of the cell on mouseover, and it works just fine in firefox and chrome, but does not work in internet explorer.
var cell_id = $(this).attr("id");
var matches = /[a-z]+(\d+)[a-z]+(\d+)/(cell_id);
var row = matches[1];
var col = matches[2];
Why doesn't this work in explorer?
In Internet Explorer, a regex cannot be used as a function. The equivalent is the exec() method, which is implemented cross browser.
var matches = /[a-z]+(\d+)[a-z]+(\d+)/.exec(cell_id);
Felt that this answer was a little incomplete without mentioning that Internet Explorer isn't the only browser that doesn't allow a regular expression to be executed like a function. In fact, it's a Mozilla extension and it's not even defined in the ECMAScript 3rd or 5th editions. You can easily check to see if it's supported using the typeof operator:
if (typeof / / == "function")
// Regex can be used like a function
else if (typeof / / == "object")
// Regex cannot be used like a function
I don't really understand why this was even implemented or why you'd even want to check for it, it's best to just err on the side of caution and use the exec method.

Categories