I'm trying to add this specific string to the end of my url on page load:
?aa_campaign=f45632
(http://examplesite.com/test.html)
It's for marketing and tracking.
I've tried this:
if window.location.href.indexOf("http://examplesite.com/test.html") {
window.location = "http://examplesite.com/test.html?aa_campaign=f45632";
}
That straight up didn't work but the idea is what I'm looking for. Any thoughts?
You can add the string to
window.location.search
instead, for a more general solution.The advantage of this approach is that you can apply this code to multiple pages with less changes to the same code.
Note that this will cause a page reload, which might not be optimal to user experience. Since you said you are doing this for tracking, you can instead ping the page by appending an image with the new url to DOM. Something like this:
Hope that helps :)
try this:
No need for
jQuery
, you can do this with pureJavaScript
in most "modern" browsers, i.e.:The pushState() method
SRC : https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history
This version will preserve any existing query string, and properly append your "aa_campaign=f45632" parameter string.
Example:
http://example.com --> http://example.com?aa_campaign=f45632
http://example.com/page.html?id=1 --> http://example.com/page.html?id=1&aa_campaign=f45632