Jquery – Trigger jQuery click event with url parameter

jquery

I have a jQuery click function on my page which triggers the opening of a feedback form div. Basically I want to specify a url that will trigger this event without it needing to be clicked. Such as foo.com?formopen

Is this possible? I'm thinking I need to pass some data through the url but not sure how to go about it.

Best Answer

Use a hash in your URL like:

foo.com#formopen

Then find that hash on load like:

$(function(){

   if (window.location.hash){
      var hash = window.location.hash.substring(1);
      if (hash == "formopen"){
         openFeedbackForm();
      }
   }

});

DEMO:

Code: http://jsfiddle.net/J5cxc/

Demo Without Hash: http://fiddle.jshell.net/J5cxc/show (no alert)

Demo With Hash: http://fiddle.jshell.net/J5cxc/show/#foobar (alert called)