Andy Dwyer
Andy Dwyer

Reputation: 716

If URL contains anything, then fire a Javascript function

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:

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;
    }
});

Upvotes: 0

Views: 1291

Answers (3)

T.J. Crowder
T.J. Crowder

Reputation: 1074959

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>

Upvotes: 4

Florian Margaine
Florian Margaine

Reputation: 60787

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

Upvotes: 2

naivists
naivists

Reputation: 33531

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>

Upvotes: 0

Related Questions