BHUVANESH MOHANKUMAR BHUVANESH MOHANKUMAR - 4 years ago 74
jQuery Question

Special characters are added while writing a string content to file using Jquery

I have a scenario, where I have to write the Json content to a file using Jquery.

I am able to write the content to the file successfully, however the file contains some special characters other than my actual contents in between some character of text.

Coding:
I have used the Content document of the Iframe where the content is saved.
Filtered the unwanted character from the text using regular expression
I was able to write the string content of Json to file.
Also enabled the feature to support the UTF & ASCII characters support in document to write.

When I open the file find some special characters.

Sample:

Unwanted character in File content

Name:Otherė— 

The actual string content does not have this character, checked during debug and only on writing to a file this characters are added.

Thanks in advance.

Code for creating a file using JQuery

var ifd = document.getElementById('scriptExport').contentDocument;
ifd.open('text/json', 'txt');
ifd.charset = "utf-8";
ifd.write(scriptContent.replace(/[^a-z0-9\s",{}:[]"]/gi, '').replace(/[_\s]/g, ' '));
ifd.close();
document.charset = "utf-8";
ifd.execCommand('SaveAs', false, scriptFileName);

Answer Source

I don't quite understand your problem but if you want to remove all special characters before parsing it to thr json just make sure to add the following code to the string :

var str= str.replace(/[^a-zA-Z0-9 ]/g, "");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download