Created
July 13, 2018 20:35
-
-
Save THEtheChad/70fda4fdd18ecd5e9f183c3d3b4d3142 to your computer and use it in GitHub Desktop.
Function for forwarding query parameters for the purpose of analytics and attribution tracking.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function prependParams(){ | |
var params = document.location.search; | |
if(!params) return; | |
params = params + '&'; | |
var nodeList = document.getElementsByTagName('a'); | |
var anchors = Array.prototype.slice.call(nodeList); | |
var l = anchors.length; | |
var q = /\?/; | |
while(l--){ | |
var anchor = anchors[l]; | |
var href = anchor.href; | |
var match = q.exec(anchor.href); | |
if(!match){ | |
match = {index: href.length}; | |
} | |
anchor.href = href.slice(0, match.index) + params + href.slice(match.index + 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment