Permalink
Browse files

Sanitize trailing ? when stripping analytics parameters from URL

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
1 parent 7471b22 commit e024bb296026c1e1dbf05ef71323253a2648896c @andytuba andytuba committed Jan 3, 2017
Showing with 4 additions and 0 deletions.
  1. +4 −0 r2/r2/public/static/js/analytics.js
@@ -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);
}

0 comments on commit e024bb2

Please sign in to comment.