Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect with javascript if an attribute "#" is appended to an url?

Tags:

javascript

How can I detect with javascript if an attribute "#" is appended to an url ?

I need a cross-browser solution. I'm currently using the following code, but it works only in Firefox:

if ((window.location.hash) || (window.location.hash != "#"))
    window.scrollBy(0,-60);

thanks

like image 831
aneuryzm Avatar asked Dec 09 '25 15:12

aneuryzm


1 Answers

First, there seems to be an error in your posted code. If you're going for the common string is neither null nor empty nor '#' pattern, it should be:

if (window.location.hash && window.location.hash != "#")
    window.scrollBy(0,-60);

That said, since Location.hash is well-supported, the code above should work on all modern browsers.

like image 130
Frédéric Hamidi Avatar answered Dec 11 '25 05:12

Frédéric Hamidi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!