简体   繁体   中英

javascript and html character escaping

I have this problem: I have a javascript, saved in a database field, that is going to be used in a web page as a href target, eg

insert into table_with_links (id, url) 
       values (1, 'javascript:var url="blö blö";.....');

// run scripts that use the database values to generate web pages

// part of the generated html code:
<a href="javascript:var url='blabla';..... </a>

So far no problems. I have german letters (Umlaute - eg ö ) in the javascript. I shouldn't save the german letters in the database, so I escape them:

insert into table_with_links (id, url) 
       values (1, 'javascript:var url="bl%F6 bl%F6";.....');

Now comes the problem - I shouldn't store the % sign in the database either, because the scripts that generate the web pages cannot handle it properly. I guess you can imagine how these scripts are 3-rd party scripts and cannot be changed.

So, my question is - can I also escape the % sign?

did you tryed this? :

var str= "remove the %";
var str_n = str.replace("%",""); 

here are the basics http://www.w3schools.com/jsref/jsref_replace.asp

then you can use an array of chars to replace take a look here javascript replace globally with array

I would suggest using oracle's built in internationalization, Oracle is capable of handling special german characters:

http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/u_i18n.htm

If you want to handle it on your own, I would suggest doing a string replace to some sequence you know:

var str = str.replace(/ö/g,"[german-umlaute]");

(the g at the end of /ö/g is to replace all occurrences in the string)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM