Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

Getting hash (#) value from URL for data element

Avatar

Avatar
Validate 1
Level 2
floriane3493069
Level 2

Likes

17 likes

Total Posts

28 posts

Correct Reply

2 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 10
Boost 1
View profile

Avatar
Validate 1
Level 2
floriane3493069
Level 2

Likes

17 likes

Total Posts

28 posts

Correct Reply

2 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 10
Boost 1
View profile
floriane3493069
Level 2

19-06-2017

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 testpages.com/#id=123 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)

Avatar

Avatar
Validate 1
Level 2
floriane3493069
Level 2

Likes

17 likes

Total Posts

28 posts

Correct Reply

2 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 10
Boost 1
View profile

Avatar
Validate 1
Level 2
floriane3493069
Level 2

Likes

17 likes

Total Posts

28 posts

Correct Reply

2 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 10
Boost 1
View profile
floriane3493069
Level 2

20-06-2017

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)

Avatar

Avatar
Establish
Employee
hyderziaee
Employee

Likes

228 likes

Total Posts

465 posts

Correct Reply

222 solutions
Top badges earned
Establish
Coach
Contributor
Shape 1
Give Back 25
View profile

Avatar
Establish
Employee
hyderziaee
Employee

Likes

228 likes

Total Posts

465 posts

Correct Reply

222 solutions
Top badges earned
Establish
Coach
Contributor
Shape 1
Give Back 25
View profile
hyderziaee
Employee

20-06-2017

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 www.abc.com/path/#id=123

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:

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

Hope this helps.

-Hyder

Avatar

Avatar
Validate 1
Level 2
floriane3493069
Level 2

Likes

17 likes

Total Posts

28 posts

Correct Reply

2 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 10
Boost 1
View profile

Avatar
Validate 1
Level 2
floriane3493069
Level 2

Likes

17 likes

Total Posts

28 posts

Correct Reply

2 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 10
Boost 1
View profile
floriane3493069
Level 2

20-06-2017

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.

Florian

Avatar

Avatar
Validate 1000
Community Manager
jantzen_belliston-Adobe
Community Manager

Likes

339 likes

Total Posts

2,331 posts

Correct Reply

819 solutions
Top badges earned
Validate 1000
Springboard
Validate 500
Validate 250
Validate 100
View profile

Avatar
Validate 1000
Community Manager
jantzen_belliston-Adobe
Community Manager

Likes

339 likes

Total Posts

2,331 posts

Correct Reply

819 solutions
Top badges earned
Validate 1000
Springboard
Validate 500
Validate 250
Validate 100
View profile
jantzen_belliston-Adobe
Community Manager

19-06-2017

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.

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

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.

Thanks,
Jantzen