1

I've seen many examples of one or the other, but none with this specific set up. I have a location object currently in my URL that opens a form via jquery: http://blah.com/contact.cfm?show_sales

$(document).ready(function(){
    if (window.location.search == "?show_sales") {
        $('#sales_form').show();
    };

I would also like a parameter to be in the URL: http://blah.com/contact.cfm?item=4445555

I've tried combinations of the two, but I don't know the proper syntax to include both. Everything I've tried won't activate the jquery.

So, what's the proper syntax for this URL?

http://blah.com/contact.cfm?show_sales&item=4445555

(this is an example of one that does not work)

2
  • http://blah.com/contact.cfm?show_sales&item=4445555 is correct for adding multiple key/value pairs to a querystring. Your JavaScript just isn’t parsing that properly. Remove the leading ? from location.search, then split it on &. Commented Nov 18, 2015 at 15:33
  • You could split location.search or use a RegExp to isolate query string fields and values Commented Nov 18, 2015 at 15:34

2 Answers 2

2

The format of URL is right. You should make some changes in the code:

$(document).ready(function(){
    if (window.location.search.indexOf("?show_sales")!=-1) {
        $('#sales_form').show();
    };

window.location.search will return show_sales&item=4445555

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

1 Comment

As I read it, this statement is basically saying "if the index of "?show_sales" is not equal to -1, show the form." Can you explain why or how the -1 index is involved?
1

You can use split:

var url = "http://blah.com/contact.cfm?show_sales&item=4445555";

var query = url.split("?")[1];

var params = query.split("&");

console.log(params); 


// params[0] = "show_sales"
// params[1] = "item=444555" and you can split again

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.