Link to home
Start Free TrialLog in
Avatar of atwoodj
atwoodj

asked on

Javascript Clean MS Word characters - Safari problem

I'm using Javascript to clean up a textarea so that proprietary characters that MS Word likes to insert, such as Smart Quotes, are removed before submitting to the database. The code that I'm using works well on all Windows browsers and Firefox for Mac, but doesn't work on Safari - Mac. Could someone please help with suggestions or alternatives?


Here's the .js code.

Thanks

var swapCodes   = new Array(8211, 8212, 8216, 8217, 8220, 8221, 8226, 8230); // dec codes from char at
var swapStrings = new Array("--", "--", "'",  "'",  '"',  '"',  "*",  "...");
function cleanWordClipboard(input) {
// debug for new codes
// for (i = 0; i < input.length; i++)
//alert("'" + input.charAt(i) + "': " + input.charCodeAt(i));
  var output = input;
  for (i = 0; i < swapCodes.length; i++) {
    var swapper = new RegExp("\\u" + swapCodes[i].toString(16), "g"); // hex codes
    output = output.replace(swapper, swapStrings[i]);
  }
  return output;
}
function cleanWord(){
  var t = document.getElementsByTagName("textarea");
  var i;
  for(i=0;i<t.length;i++){
    t[i].value = cleanWordClipboard(t[i].value);
  }
}

Avatar of third
third
Flag of Philippines image

try replacing,

t[i].value = cleanWordClipboard(t[i].value);

with

t[i].innerHTML = cleanWordClipboard(t[i].innerHTML);
Avatar of atwoodj
atwoodj

ASKER

third
Thanks for the comment. Unfortunately it didn't work. With the coding you suggested, the script no longer works on FF-Win.
ASKER CERTIFIED SOLUTION
Avatar of third
third
Flag of Philippines 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
Avatar of atwoodj

ASKER

I've updated the code to your suggestion. My Mac tester will run it through the paces when he gets off work tonight. I will keep you posted. Thanks.

Any folks with Macs out there want to help out testing, please let me know!
Avatar of atwoodj

ASKER

I've done the testing and it seems to be working.

Thanks very much!
welcome. ;-)