Skip to content

Commit

Permalink
Sanitize trailing ? when stripping analytics parameters from URL
Browse files Browse the repository at this point in the history
Safari doesn't remove the ? from a URL when setting `url.search = ''`
which results in links not correctly applying `:visited`.

For example, before this change:
https://www.reddit.com/?utm_foo=bar -> https://www.reddit.com/?
and links to https://www.reddit.com/ don't render as :visited

After this change,
https://www.reddit.com/?utm_foo=bar -> https://www.reddit.com/
and links to https://www.reddit.com/ are properly empurpled.
Additionally, hash is preserved:
https://www.reddit.com/?utm_foo=bar&baz=qux#hoopla -> https://www.reddit.com/?baz=qux#hoopla
  • Loading branch information
jewel-andraia committed Jan 4, 2017
1 parent 7471b22 commit e024bb2
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions r2/r2/public/static/js/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ r.analytics = {
var a = document.createElement('a');
a.href = window.location.href;
a.search = $.param(strippedParams);
if (!a.search) {
// Safari leaves a trailing ? when search is empty
a.href = a.href.replace(/\?(#.+)?$/, a.hash);
}

window.history.replaceState({}, document.title, a.href);
}
Expand Down

0 comments on commit e024bb2

Please sign in to comment.