0

I have the following JavaScript code:

var matchArray = [];
var passedUrl = '/' + url;
var tabLink;

$('.uiAjaxTabs li a').each(function () {
    if (passedUrl.indexOf($(this).attr('href')) == 0) {
        boverlap_penalty = passedUrl.replace($(this).attr('href'), '').length;
        matchArray.push({ 'score': boverlap_penalty, 'dom_obj': this });
    }
});

if (matchArray.length) {
    tabLink = matchArray.sort(function (a, b) {
        return (a.score < b.score) ? -1 : 1
    }).shift().dom_obj;
}

$(tabLink).parents('li').addClass('loading');

Which takes a passedUrl and then matches it up with a set of links to see which most closely matches the url, and then adds a class of loading to it.

This works fine EXCEPT if the link has a space in it e.g. domain.com/People?Name=John Doe because the browser sees it as domain.com/People?Name=John%20Doe and therefore doesn't match it correctly when the passedUrl has the escaped spaces and the link does not.

Any ideas on how to fix this?

Cameron
  • 27,963
  • 100
  • 281
  • 483

2 Answers2

1

Any ideas on how to fix this?

Use

var passedUrl = decodeURI('/' + url);

See MDN docs.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

Try JavaScript's unescape function, it seem to decode URL-encoded strings.

artahian
  • 2,093
  • 14
  • 17
  • Check [decodeURIComponent vs unescape, what is wrong with unescape?](http://stackoverflow.com/q/619323/1048572) – Bergi Jun 27 '13 at 15:51