Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript detect # in url

Tags:

javascript

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?

like image 267
Starlin Avatar asked Sep 12 '10 14:09

Starlin


People also ask

Can JavaScript do browser detection?

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.

Which tag is used to detect JavaScript?

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.

Which JavaScript is used for browser detection?

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.


1 Answers

Querying the hash property before setting it should do the job.

if ((!window.location.hash) || (window.location.hash == "#"))
 window.location.hash = "tophash";
like image 181
Pekka Avatar answered Oct 20 '22 23:10

Pekka