简体   繁体   English

javascript和html字符转义

[英]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 我有这个问题:我有一个JavaScript,保存在数据库字段中,该JavaScript将在网页中用作href目标,例如

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. 我在javascript中有德语字母(Umlaute-例如ö )。 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. 我想您可以想象这些脚本是3方脚本并且无法更改。

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 这是基础知识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 那么您可以使用一个字符数组来替换,在这里看看javascript用array全局替换

I would suggest using oracle's built in internationalization, Oracle is capable of handling special german characters: 我建议使用Oracle内置的国际化版本,Oracle能够处理特殊的德语字符:

http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/u_i18n.htm 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) (/ö/ g末尾的g替换字符串中所有出现的字符)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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