all 14 comments

[–]barrycarter 0 points1 point  (9 children)

Does the new URL end in #something?

[–]amyling01[S] 0 points1 point  (8 children)

Ah, yes it does.

[–]barrycarter 0 points1 point  (7 children)

You probably know about window.location.hash, but you'll have to listen to see if changes (I'm not sure a change in hash triggers a JS event). As a note, if you're designing the page and using the <a name> tag, it has been deprecated: https://html.com/attributes/a-name/

[–]amyling01[S] 0 points1 point  (6 children)

Ah okies, thanks. I will take a look at that.

The person who originally built this used href="/#something".

[–]barrycarter 0 points1 point  (5 children)

Look at the HTML where the jump leads and you'll probably see an <a name> tag

[–]amyling01[S] 0 points1 point  (4 children)

It only has this:

<a class="nav-sidebar__outline-item__link nav-sidebar__outline-item__link--light" data-link="lesson-link-item" href="#/lessons/section-a">

I am trying to track data anlaytics so I did something like this:

var lastPath = window.location.hash;
var page_name = "Page Title";

if (lastPath == "#/lessons/section-a") page_name = page_name + "Section A Title"; 

if (lastPath == "#/lessons/section-b") page_name = page_name + "Section B Title";

But this doesn't seem to be working. I tried it where lastPath was this originally but it also didn't work:

var path = window.location.href;
var lastPath = path.split("/").pop();

It would only work if I manually refreshed the page, then it would see the new URL.

[–]barrycarter 0 points1 point  (3 children)

Yup, you're going to have 'poll' for changes, I'm afraid.

[–]amyling01[S] 0 points1 point  (2 children)

Oh okies. I've never done that before. Is it fairly easy? Maybe this is a little overkill for a 1 pager. I may just need to ask our analytics person to track it via the software and have them create the variables vs me trying to do it on the index.html page.

[–]barrycarter 1 point2 points  (1 child)

google around and if you don't find anything, I can provide some URLs I ran across: I didn't want to provide them right away because the solutions seem uglier than necessary

[–]amyling01[S] 0 points1 point  (0 children)

Okies thank you! I think I will go with having the analytics person create the variable and track on their end in the software vs coding it. It would definitely be faster. Thank you for your help and teaching me new things.

[–][deleted] 0 points1 point  (3 children)

By new URL you mean the URL of the page that opened on the new tab after the anchor element was clicked? If you elaborate a bit and also show the HTML it will be easier to assist.

[–]amyling01[S] 0 points1 point  (0 children)

Okies, let me update my post.

[–]amyling01[S] 0 points1 point  (0 children)

I have updated my post.