gal007
gal007

Reputation: 7192

Compare two Strings with different encoding

I'm writing a Greasemonkey script. I need to compare two strings, where one of these is equal to document.location.href.

If document.location.href is equal to "http://lema.rae.es/drae/?val=ñáñara" then I need to do something extra, but I can't determine if the two strings are equal, because document.location.href is converted to another character set. This is an example:

var currentLocation = document.location.href.toString();
var targetLocation = 'http://lema.rae.es/drae/?val=ñáñara';

alert(currentLocation + '\n' + targetLocation);

/* OUTPUT:

    http://lema.rae.es/drae/?val=%C3%B1%C3%A1%C3%B1ara
    http://lema.rae.es/drae/?val=ñáñara
*/

How can I convert two strings to the same character set?

Upvotes: 0

Views: 1214

Answers (2)

El Hocko
El Hocko

Reputation: 2606

or the other way round: encodeURI("http://lema.rae.es/drae/?val=ñáñara")

Upvotes: 1

ZER0
ZER0

Reputation: 25322

Just use:

var url = decodeURI(window.location.href);

Should be enough.

See: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/decodeURI

Upvotes: 3

Related Questions