How can I collect current page URL in form?


#1

Hello,

Does anyone know if there’s a way to put a hidden text field in a Hubspot form and have it collect the current page url when the user submits the form? (through javascript or otherwise?)

I feel like this is pretty basic but I’m not sure where to start. Thanks.


#2

Hi @Shadministrator,

The current page (or location) of the browser is stored in Window.location (see below). You could populate a hidden form field with this value by targeting the input using jQuery and setting the value to window.location. The form embed document below has some examples of this.


#3

Ok, thanks for that Derek.

So I did see this in the documentation - https://developers.hubspot.com/docs/methods/forms/advanced_form_options

I’m thinking I could just do:

$('input[name="website"]').val(Window.location.href).change();

The question is where do I put this code? I tried putting it in the embed code but it didn’t work. I feel like I’m missing something (or a few things). Any other advice on this greatly appreciated. Thanks.


#4

Hi @Shadministrator,

You could pass an onFormReady callback function in the form embed code, and execute that within that callback. That should populate the hidden form field once the form has been rendered in the DOM. It’d look something like this:

hbspt.forms.create({
  portalId: '{your Hub ID}',
  formId: '{your form GUID}',
  onFormReady: function($form) {
    $('input[name="website"]').val(Window.location.href).change();
  }
});

#5

AWESOME!

That works great, thanks Derek. Only thing I changed was the “Window.location.href” --> “window.location.href” (made the “W” lowercase).

Hope this helps someone else.


#6

Thanks for the help guys. I cant seem to get my code to work with these steps above. Here is what I got:
hbspt.forms.create({
portalId: ‘XXXXXX’,
formId: 'XXXX’
onFormReady: function($form) {
$(‘input[name=“sample_pn_url”]’).val(Window.location.href).change();
}
});

sample_pn_url is the hidden field I am trying to write the URL to.

Any help would be great!


#7

Hi Venkie,

Make sure the “w” in “Window.location.href” is lowercase (should be “window.location.href”).

Also, make sure “sample_pn_url” is the field id not the field name.


#8

@Shadministrator

Thanks for the quick reply and fixes. I changed the W to w, and I am still getting no luck. I checked that the sample_pn_url is the id. Any other suggestions?

hbspt.forms.create({
portalId: ‘XXX’,
formId: ‘XXXXXX’,
onFormReady: function($form) {
$(‘input[name=“sample_pn_url”]’).val(window.location.href).change();
}
});


#9

Venkie, did you get this to work? Often it’s the input name that spoils the party. To clarify, it’s not the “id”, it’s the “name”. Good idea to view the form source code and see if the name matches with your script.

Here’s an example of a working configuration:

hbspt.forms.create({
portalId: ‘xxxxxxxx’,
formId: ‘3f986685-13c9-4689-bfb6-3cb109b3169c’,
css: ’ ',
onFormReady: function($form){$(‘input[name=“last_conversion_location”]’).val(window.location.href).change();}
});

Of course I created a new conversion property “last conversion location”, which generated the field “name” last_conversion_location then added the hidden field on the form.

Let me know if you still need help.


#10

Hi!

I am not reaching the goal :frowning:

<!--[if lte IE 8]>
  <script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2-legacy.js"></script>
  <![endif]-->
  <script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js"></script>
<script>
  hbspt.forms.create({
  portalId: 'XXXXXX',
  formId: '72b74198-7eda-41db-b85e-26720205d7a1',
  onFormReady: function($form){$(‘input[name=“website”]’).val().change(window.location.href);}
});
});
</script>

What is wrong with my code`?


#11

Hi Patrick,

Make sure you have your own form id and the input name is correct (is the
contact property you are trying to populate named “website” ?)


#12

label and internal name is “website” and the form must be the right, otherwise the integration with HTML would not show this form or?

http://www.sorba.ch/kontakt-test
p: testSorba


#13

Gonna go out on a limb here and suggest renaming your custom URL tracking field something besides “website”. HS already has a field for Website URL with an internal name of “website”. Try using the code I pasted above and rename your field to “last_conversion_location”.


#14

Thank you all for the attempts to help.
HubSpot had a cache problem. My custom html box was not even loaded, so referring to the variable never would be possible. Deleting the whole module and setting up a new html box helped.

Perhaps others need this information too :wink: Later, the code from das_kreitz works perfectly.


#15

Thanks @Derek_Gervais - but window.location.href doesnt work within a cross domain iframe.