My application is an iframe app so when the user changes page they do not automatically get taken to the top. To combat this on page load I call
window.location.hash = 'tophash'
I have found however on some rare cases I need to take the user to a specific part of the page. So I make a url with #anotherID on the end. Problem is currently they are taken to tophash on page load.
What I need is if there is a hash within the url it does not run window.location.hash = 'tophash'
So my question is... how do I detech the presence of a # in the url?
How to detect the user browser ( Safari, Chrome, IE, Firefox and Opera ) using JavaScript ? The browser on which the current page is opening can be checked using JavaScript. The userAgent property of the navigator object is used to return the user-agent header string sent by the browser.
The Complete Full-Stack JavaScript Course! To detect if JavaScript is disabled in a web browser, use the <noscript> tag. The HTML <noscript> tag is used to handle the browsers, which do recognize <script> tag but do not support scripting. This tag is used to display an alternate text message.
JavaScript has a standard object called navigator that contains data about the browser being used. The navigator object has a lot of properties, but the . userAgent property — a string that contains data about the browser, operating system, and more– is all we'll need.
Querying the hash
property before setting it should do the job.
if ((!window.location.hash) || (window.location.hash == "#"))
window.location.hash = "tophash";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With