Getting hash (#) value from URL for data element



Hi there,

I am using DTM and I wanted to create a data element using the hash (#) value from an url.

If the url is I would like to geht the "123" as value for my data element.

I tried window.location.hash.substring but it does not work. Can anybody help me with that? Any other ideas how to solve this problem?

Thank you very much.

Best Florian

Accepted Solutions (1)

Accepted Solutions (1)



Hi guys,

thank you for your great support. I made a mistake. I tried to put to code directly into the js object field. But then I put the code into the custom script editor. It looks like this now:

return document.URL.substr(document.URL.indexOf('#')+4); 

And it works 🙂

Thanks a lot.

Best Florian

Answers (3)

Answers (3)




Hi Florian,

Can you give a general structure of the URL?

Would you have other parameters apart form id after #?

I am talking your URL to be

You can run the following function:

function id(){

  var x = document.URL.substr(document.URL.indexOf('#')+1);

  return x.substr(x.indexOf('=')+1);

  return y;


You can also run a single line of function:


Hope this helps.




Hi Jantzen,

thank you for your quick response. Unfortunatelly it does not work. If I put your code in the data element I only get the default value.

If I use location.hash it works perfectly well but then I get  whole anchor part of the URL, including the hash sign (#) 😕

Any other ideas?

Thank you.



Community Manager


Hi Florian,

Do you have URL where we can test this?

I'm not a developer, but it looks like the code below might get you what you'd like. Try plugging this into the console on the page to see if it returns the correct value.


If it does, you can put the code it in a data element and see if _satellite.getVar('data_element_name') returns the expected value.