How can I collect current page URL in form?



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.


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.


Ok, thanks for that Derek.

So I did see this in the documentation -

I’m thinking I could just do:


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.


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:

  portalId: '{your Hub ID}',
  formId: '{your form GUID}',
  onFormReady: function($form) {



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.


Thanks for the help guys. I cant seem to get my code to work with these steps above. Here is what I got:
portalId: ‘XXXXXX’,
formId: 'XXXX’
onFormReady: function($form) {

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

Any help would be great!


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.



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?

portalId: ‘XXX’,
formId: ‘XXXXXX’,
onFormReady: function($form) {


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:

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.



I am not reaching the goal :frowning:

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

What is wrong with my code`?


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” ?)


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


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”.


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.


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


Is there a reason why the form_page_url token would work on one domain and not a secondary domain?

Using the code in this post helps collect the current url a form is submitted on in my primary domain. However, using the same exact setup, on a secondary domain it doesn't.

I even created a second property to track separately from the the token on the primary domain and its not functioning either.

My code:

<!-- [if lte IE 8]>
        <script charset="utf-8" type="text/javascript" src="//"></script>
        <script charset="utf-8" type="text/javascript" src="//"></script>
        	portalId: "XXX",
        	formId: "XXXXXX",
                css: '',
          onFormReady: function($form) {

Browser console does through this error:
TypeError: $ is not a function
at onFormReady (
at Object.runCustomerCallbackFunction (v2.js:9)
at t.componentDidMount (v2.js:11)
at r.notifyAll (v2.js:1)
at r.close (v2.js:3)
at r.closeAll (v2.js:4)
at r.perform (v2.js:4)
at f (v2.js:3)
at r.perform (v2.js:4)
at Object.batchedUpdates (v2.js:2)


Hi @marioSWP,

That error means that the jQuery function call (i.e. $()) is undefined. I'd expect to see that error if you didn't have jQuery on your secondary domain.