You can grab the current URL with window.location.href
, so if you grab the href
on the landing page, then you have the landing URL.
You can grab the referring URL with document.referrer
. This also needs to be grabbed on the landing page.
I haven't used jQuery in a while, but I believe the syntax would be something like this:
// For this example I have assumed your fields have the IDs landingURL and referralURL.
$('#landingURL').val(window.location.href);
$('#referralURL').val(document.referrer);
Now, if the landing page is not the same page as the form, and you need to pass this information along to the form page using only JavaScript in the browser, then I would suggest that you grab the landing page URL and the referral URL as described above and save them in either localStorage
or sessionStorage
.
Here's an example of how you can do that:
// On the landing page:
localStorage.setItem('landingURL', window.location.href);
localStorage.setItem('referralURL', document.referrer);
// On the form page:
$('#landingURL').val(localStorage.getItem('landingURL'));
$('#referralURL').val(localStorage.getItem('referralURL'));
Hope that helps!