Basically, if the URL/window.location contains absolutely any variable whatsoever (past domain.com/, of course), I’d like javascript to execute something.
Currently, I have the following jQuery code which only executes when window.location contains the exact wording “#hash”, but as stated before I’d like to expand the functionality for all variables.
Edit: Sorry, to clarify, by variable I mean any one of the following examples:
- domain.com/#hash
- domain.com/#hash2
- domain.com/sub/folder
- domain.com/textwithoutahash
Also, if someone knows how to do this in basic Javascript and without the need for the jQuery library, that would be an added bonus!
$(function() {
if ( window.location.href.indexOf('#hash') > -1 ) {
myfunctionhere;
}
});
See update at end re your clarification
Put the script at the end of the page, just before the closing
</body>, and:If by “variable” you mean a document fragment identifier (“hash”), then:
If by “variable” you mean a query string, then
If by “variable” you mean a resource name, e.g., not
http://domain.combuthttp://domain.com/page, then:More on the location object on MDN.
Re your clarification:
Those examples come down to having either
hashorpathnameor both, so:…and of course, if you also wanted to handle
http://domain.com?foo=bar, then add insearchas well: