Add string to url to load page using jQuery?

I am trying to add this particular line to the end of my url when the page loads:

? Aa_campaign = f45632

( http://examplesite.com/test.html )

This is for marketing and tracking.

I tried this:

if window.location.href.indexOf("http://examplesite.com/test.html") { window.location = "http://examplesite.com/test.html?aa_campaign=f45632"; } 

This does not directly work, but the idea is what I am looking for. Any thoughts?

+6
source share
4 answers

There is no need for jQuery , you can do this using pure JavaScript in most "modern" browsers, that is:

 if (window.location.href === "http://examplesite.com/test.html") { window.history.pushState("object or string", "Title", "http://examplesite.com/test.html?aa_campaign=f45632"); } 

PushState () method

pushState () accepts three parameters: a state object, a header (which is currently ignored), and (optionally) a URL. Let's look at each of these three parameters in more detail:

 state objectThe state object is a JavaScript object which is associated with the new history entry created by pushState(). Whenever 

the user enters a new state, the popstate event is triggered, and the state property of the event contains a copy of the history object's state record.

 The state object can be anything that can be serialized. Because Firefox saves state objects to the user disk so they can be restored 

after restarting the browser, we will impose a limit on the size of 640 thousand characters in the serialized representation of the state object. if you pass a state object whose serialized representation is larger than that for pushState (), the method will throw an exception. If you need more space than this, you are advised to use sessionStorage and / or LocalStorage.

 title — Firefox currently ignores this parameter, although it may use it in the future. Passing the empty string here should be safe 

against future method changes. In addition, you can pass a short header for the state you are moving to.

 URL — The new history entry URL is given by this parameter. Note that the browser won't attempt to load this URL after a call to 

pushState (), but it may try to load the URL later, for example after restarting the user. The new URL does not have to be absolute; if it is relative, it is resolved relative to the current url. The new URL must have the same source as the current URL; otherwise, pushState () throws an exception. This parameter is optional; if not specified, it is set to the current URL of the document.

SRC: https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history

+7
source

Instead, you can add a line to window.location.search for a more general solution.

 if (location.origin + location.pathname === 'your url here') { location.search += 'additional data here'; } 

The advantage of this approach is that you can apply this code to multiple pages with fewer changes in the same code.

Please note that this will result in a page reload, which may not be optimal for the user. Since you said you were doing this for tracking, you can instead ping the page by adding an image with a new URL in the DOM. Something like that:

 var pinger = new Image(0,0); // keep the size of the image 0 pinger.src = 'changed url here, with the new query param appended'; document.body.appendChild(pinger); // ping sent pinger.parentNode.removeChild(pinger); // remove the node when ping is sent 

Hope that helps :)

+3
source

try the following:

 if (window.location.href.indexOf("http://examplesite.com/test.html" !== -1) { window.location = "http://examplesite.com/test.html?aa_campaign=f45632"; } 
0
source

This version will save any existing query string and correctly add the parameter string "aa_campaign = f45632".

 var w = window.location; if (w.search.indexOf("aa_campaign") == -1) { window.location = w + (w.search.indexOf("?") == -1 ? "?" : "&") + "aa_campaign=f45632"; } 

Example:

0
source

Source: https://habr.com/ru/post/985363/


All Articles