简体   繁体   中英

JavaScript replace behaves differently in Chrome to IE

The following JavaScript works perfectly in Chrome to replace a string:

myUrl = someUrl.replace('%2C%7B%22itn%22%3A%5B%22%20guidelines%20%22%5D%7D', '');

but in Internet Explorer it fails to replace the string.

It seems that it is related to how IE treats %22 as if I debug this code and replace %22 with " it works in debug. If I try doing this in the script itself it then errors in both Chrome and IE.

How can I make this script work cross browser?

It's a known bug in Internet Explorer that it doesn't encode " in the query string part of a URL. Try replacing URLs both when %22 and " are used.

For example, it is possible to do this using a regular expression :

myUrl = someUrl.replace(/%2C%7B%22itn(?:%22|")%3A%5B(?:%22|")%20guidelines%20(?:%22|")%5D%7D/, '');

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