2

Is there a way to check the next URL a user visits? Basically I have a dropdown menu with a list of options for delivery dates, when you select a date and press a submit button that selected date with go into a spreadsheet I made.

However I have been trying to code something what will only send that selected date into my spreadsheet if they meet a certain URL, so I don't have to have that submit button. So if they go to checkout and the URL contains "checkout" their selected date from the previous page will go into my spreadsheet. This is all on the same website btw.

  • `window.locaction.href` gets you the current url. You could use a cookie or [local storage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) to keep the previous url. You could use an onchange listener on the dropdown that loops over all `` tags on the current page & appends the selection as a query paramater, then on the checkout page check if the query paramater is set... Umm. There's probably other ways you could approach it too. – Reed Nov 29 '20 at 19:24
  • 1
    Edit the question to detail exactly what you have tried; turned into a minimal, complete, verifiable example to make it easy to understand. And explain why it didn’t solve your problem. – PA. Nov 29 '20 at 19:27
  • 1
    @Reed Thanks for the reply. Could I use the page with the selection menu to check for the next URL though? Like it waits for a redirect, and inspects the redirect URL before leaving onto the next page? Or is this not possible? – ineedhelpplease Nov 29 '20 at 19:34
  • Does this answer your question? [How to call a function before leaving page with Javascript](https://stackoverflow.com/questions/28627111/how-to-call-a-function-before-leaving-page-with-javascript) – A. Meshu Nov 29 '20 at 19:45
  • @A.Meshu Thanks for the reply. Though that would sort of work, I can't guarantee a user leaving would be to go to the checkout page. They could just be leaving the website. I need to know whether the next page they go to is the checkout url or not if you get me. Because if it is the checkout page then that's when my script will come into play – ineedhelpplease Nov 29 '20 at 19:50
  • @ineedhelpplease i don't think that you can if it is not on your server. – A. Meshu Nov 29 '20 at 19:55
  • @A.Meshu hm okay. Could I not see what the link is they're going to before they leave? An example for this would be on DeviantArt, if you click on an external link, it will show a page warning a user they are leaving to go to a website outside of DeviantArt. Surely they check the link or is that also because it's their own server? – ineedhelpplease Nov 29 '20 at 19:58
  • I'd say this is a very broad question and won't get an answer unless you point some constraints. You could replace all the links on the page with spies, but then the user might go to a link directly (eg, from a bookmark). If you're interested in links to your server only, then server-side behaviour would be your best bet. – Christian Nov 29 '20 at 20:00
  • Regarding the DeviantArt example, it's like I mentioned with spies. Instead of having ` – Christian Nov 29 '20 at 20:03
  • @Christian thank you for the reply. The only problem I have is this website is run using Shopify, I can't edit the checkout button to check the redirector. I guess I will have to see about editing the checkout button, if I can that is. – ineedhelpplease Nov 29 '20 at 20:10
  • What about setting click listeners on all the links on the page? And an onsubmit listener on the forms? `document.querySelector('a').addEventListener('click',function(event){console.log(event.target.href);});` except you'd `querySelectorAll` & `foreach` it. I don't remember the clean way to chain that in js. – Reed Nov 29 '20 at 20:15
  • @Reed I don't know much about click listeners but I am going to see if I can find when a user clicks the checkout button using queryselector, as the checkout button uses a certain class. I will do some experimenting, thank you. – ineedhelpplease Nov 29 '20 at 20:34

1 Answers1

1

I didn't find a way to exactly find the next URL a user visits, but I did find a way around it (sort of). Using querySelector and an event listener to send the script when a user clicks the checkout button.

document.querySelector("button").addEventListener("click", function() ...
isherwood
  • 58,414
  • 16
  • 114
  • 157