0

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;
    }
});
11
  • 1
    Bonus for: "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!" Commented Jan 25, 2013 at 7:29
  • As written, you don't need the jQuery library. Commented Jan 25, 2013 at 7:30
  • $(function() {}); Is actually a jquery shortcut, so, yes you do need jQuery. Commented Jan 25, 2013 at 7:31
  • 2
    You don't want something like domain.com/?some=variable too? Commented Jan 25, 2013 at 7:39
  • 1
    @Adam but you specifically said "as written, you don't need jQuery". And I was saying that "as written" he does, since he's using $(function() {}) Of course it can be written in Vanilla javascript, everything can. But "AS WRITTEN" above, he needs jQuery. Commented Jan 25, 2013 at 14:55

3 Answers 3

4

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:

<script>
if (location.hash) {
    callYourFunction();
}
</script>

If by "variable" you mean a query string, then

<script>
if (location.search) {
    callYourFunction();
}
</script>

If by "variable" you mean a resource name, e.g., not http://domain.com but http://domain.com/page, then:

<script>
if (location.pathname && location.pathname !== "/") {
    callYourFunction();
}
</script>

More on the location object on MDN.


Re your clarification:

Edit: Sorry, to clarify, by variable I mean any one of the following examples:

Those examples come down to having either hash or pathname or both, so:

<script>
if ((location.pathname && location.pathname !== "/") || location.hash) {
    callYourFunction();
}
</script>

...and of course, if you also wanted to handle http://domain.com?foo=bar, then add in search as well:

<script>
if ((location.pathname && location.pathname !== "/") ||
    location.search ||
    location.hash) {

    callYourFunction();
}
</script>
Sign up to request clarification or add additional context in comments.

9 Comments

@FlorianMargaine: I'm not following you. hash works reliably regardless of whether there's a resource name, and the hash is not included in search or pathname.
@Ian: Thanks. Yeah, at first I took the word "variable" too literally and assumed search. :-)
@T.J.Crowder Haha yeah, I assumed a lot too. It would've been nice if we had gotten the information beforehand that is now in the question from these edits
Well, after your last edits, I'd better recommend my answer :P
@AndyDwyer I'd recommend this solution, since the filter function I use in my answer doesn't work in IE8. You could add a shim or use jQuery's filter, but TJ's answer is good enough to not need an external dependency.
|
2

You could check if there is a hash, a pathname or a search.

Or, to simplify, you could simply use this:

if (window.location.href.split('/').filter(Boolean).length > 2) {
    callYourFunction();
}

window.location.href is simply the whole URL. If there's something after the domain, it'll be shown.

This function will be triggered for the following cases:

  • domain.com/some/path
  • domain.com/#hash
  • domain.com/?some=variable

2 Comments

"simplify"? I think not. :-)
@T.J.Crowder Well, I'm just checking one property :P
0

You could check if search property of window.location is set to something. Also, you can check the hash property:

if (window.location.search || window.location.hash) {
  yourfunctionhere();
}

To invoke it without jQuery, just include it in an 'onload' script:

<script type='text/javascript'>
  document.onload = function () {
    if (window.location.search || window.location.hash) {
      yourfunctionhere();
    }
  }
</script>

2 Comments

This only includes ?key=value and #hash...not /path/to/page
the /path/to/page wasn't clearly mentioned in the original post :)

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.