Getting referring URL from sticky bar - Javascript

  • 7 September 2021
  • 5 replies
  • 11 views

Hey all,

I am trying to edit the URL in a button in my sticky bar. The button links to a Typeform, and I want to append the URL with a field #source=“url of referring page”.

However, since the sticky bar is in a different iFrame, I cannot get the URL of the parent page (cross-origin requests denied).

Does Unbounce have a way for me to get the URL of the page that the sticky bar is loading on, in javascript?

Thanks!


5 replies

Badge

I don’t have access to an account with sticky bars enabled, but I think they automatically pass through any URL parameters from the parent page. On the parent page, I think you could do something like:

(function(){
  var parentURL = document.location.href;
  var newLoc = document.createElement("a");
  
  newLoc.href = parentURL;
  newLoc.search += "&source=" + encodeURIComponent(parentURL);
  
  history.replaceState({},null,newLoc.href);
})();

prior to loading your sticky bar. That would pass through to your sticky bar and you could choose the option to pass through URL parameters on your button. Then you’d be able to grab that from the URL of your Typeform and pass it to a hidden field.

Thats a great idea @tsprinkel !

Follow up question - how would I make sure that my code loads before the sticky bar?

Badge

Just put this code before the sticky bar embed code on the parent page.

Thanks @tsprinkel - this has been really helpful. I’m just getting stuck at one point: I am not sure how to grab the URL parameters from the Typeform and pass it to the hidden field. I realize this is more of Typeform question, so please let me know if this is not the appropriate place to ask. Thanks for all your help regardless!

Badge

Definitely a Typeform question, but it looks like their syntax to fill in hidden fields is [url]#[hidden field name]=[hidden field value]. If the pass through URL parameters option doesn’t work, you could just parse it out directly in your sticky bar and manipulate it that way. However, it might be worth testing to see if altering the code to be newLoc.hash instead of newLoc.search would work. I don’t know if Unbounce passes through everything or just search params.

Reply