We help IT Professionals succeed at work.

Javascript Clean MS Word characters - Safari problem

atwoodj
atwoodj asked
on
1,377 Views
Last Modified: 2010-05-18
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);
  }
}

Comment
Watch Question

Commented:
try replacing,

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

with

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

Author

Commented:
third
Thanks for the comment. Unfortunately it didn't work. With the coding you suggested, the script no longer works on FF-Win.
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
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!

Author

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

Thanks very much!

Commented:
welcome. ;-)
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.