Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger jQuery click event with url parameter?

Tags:

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.

like image 698
caffeinehigh Avatar asked Feb 01 '26 08:02

caffeinehigh


1 Answers

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)

like image 123
Curtis Avatar answered Feb 04 '26 00:02

Curtis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!