0

I want to write a script in JS to tell if the URL has the # st like https://hoogabooga/t/wow.html#btn1 so I can have it can change the background color of that element.

Because I'm making the pages non-scrollable and small I need to make making pages that extended part of each type of content... (If more was supposed to be there) and making buttons to go to the content on the new page that is that of finish what was left off, and the way I know how to do that is to send them to a page wow.html with the #btn1 but the page can't scroll.

1
  • Sorry, I can't say I follow. You want to see if the URL has a # (hash) in it? There's the hashchange event, or you can check if the # symbol exists with String.prototype.includes Commented Nov 23, 2020 at 22:55

2 Answers 2

1

if you want to detect a change to the hash:

function hashHandler() {
  console.log('The hash has changed to ' + location.hash);
}

window.addEventListener('hashchange', hashHandler, false);

if you just want to see what is the current hash: console.log(location.hash)

Sign up to request clarification or add additional context in comments.

Comments

0

You can get the hash value from window.location.hash.

For example.com/#btn1, window.location.hash would contain btn1.

It sounds like you're trying to respond to hash changes though, like putting a hash in an anchor's href attribute. For that, you want window.onhashchange.

window.onhashchange = function(){
    var newHash = window.location.hash;

    if(newHash == "something")
    { 
        //load new page 
    }
}

For the record, this can be accomplished just as easily with click events.

<a href="#" onclick="doSomething()">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.