2

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.

0

2 Answers 2

11

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)

Sign up to request clarification or add additional context in comments.

1 Comment

this answer solved a tricky UI challenge for me and deserves many more upvotes
0

Hey there are many ways to do this.

Method One - Using Query String params

In your url pass in a query string value which indicates that the form should be opened.

So your url would be

www.foo.com?feedback=true

Then in JS you can do:

var params = {};
var ps = window.location.search.split(/\?|&/);
for (var i = 0; i < ps.length; i++) {
    if (ps[i]) {
        var p = ps[i].split(/=/);
        params[p[0]] = p[1];
    }
}
if(params.feedback){
     $(".feedback-div").show();
}

Method Two - Using a hash-tag in the url

Your url will be like:

www.foo.com#feedback

In your JS:

if(window.location.href.indexOf("#feedback") > -1) {
     $(".feedback-div").show();
}

Comments

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.