0

I have a link in an e-mail that points the user to a webpage. On this webpage there is a pop-up that is hidden, unless a button is clicked; then jQuery displays that div.

I have searched for a way to grab the query string from the URL with jQuery to no avail. I know with PHP it would be a simple $query = $_GET['query'], but I'm not sure how to accomplish this with jQuery.

I need to check for a query string, say "?popUp=true", and display that div with jQuery if the query string is present.

Is there a way I can pass the PHP GET variable to jQuery? I'm confused.

Banjerr
  • 121
  • 2
  • 13

2 Answers2

0

jQuery has nothing to do with it; it's just plain Javascript:

var query = window.location.search;

Or simply location.search. Do window.location from the Javascript console to see what all is there.

So:

$(document).ready(function(){
    if (location.search.indexOf('popUp=true') > -1)) {
        doPopUp(); // run your code.
    }
});

MDN

Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
  • I saw this solution similarly [here](http://stackoverflow.com/questions/4656843/jquery-get-querystring-from-url). But I'm still not clear on how jQuery can check if javascript found the query string? – Banjerr Aug 28 '14 at 01:45
  • On page load/ready, check for your query (`if (window.location.search.indexOf('your=stuff') > -1)`) and run the code that opens your pop-up. Note, if that's a *window* pop-up, running that automatically is going to be difficult (since window pop-ups are, y'know frowned on and generally hated). – Jared Farrish Aug 28 '14 at 01:47
  • This seemed promising, but I can't quite figure out why its not currently working. `` – Banjerr Aug 28 '14 at 02:25
  • You're using `document.getElementById()` incorrectly; you would only pass one ID text at a time (`optionBG`). However, use jQuery: `$('div#optionBG, div#optionBG2').show();`. – Jared Farrish Aug 28 '14 at 11:42
0

Of course, we can get query parameter from url by split it, and it's pure java script. There's a general way to do it: http://www.jquerybyexample.net/2012/06/get-url-parameters-using-jquery.html

But if you just need [popup=true] to use with jquery, you'd better to use hash.

URL:

http://.../yourscript.php#popup

And your jquery code:

if(window.location.hash) {
 // Fragment exists, show popup
} else {
  // Fragment doesn't exist, do nothing
}
teddy
  • 534
  • 5
  • 13