How can I automatically show a jquery colorbox on my page when a certain $_GET variable is passed?
I have a jquery colorbox (lightbox) that pops up when users click a button on my page. Under certain conditions though i want this colorbox to appear without the user having to click a button. For example when the page is loaded and variable is passed in the query string I want to pop up the colorbox.
For example the following code shows how when user clicks the signup button the colorbox appears (this is for a page called example.php)
<p class="signup_button"><a href="#" class="free_signup_link"><img src="images/buttons/sign_up_now.gif" alt="Sign Up Now"></a></p>
<script type="text/javascript">
$('.free_signup_link').colorbox({href:"signup.php?type=5"});
</script>
Wh开发者_JS百科at I want to do is if the page is loaded with a variable in the query string then the colorbox is automatically shown (eg for example.php?show=1)
if($_GET['show'] == "1") {
// show the color box
}
Anyone know how to do this?
thanks
This should work, it's probably considered a bit "dirty" however.
<?php
if($_GET['show'] == "1") { ?>
<script type="text/javascript">
$.colorbox({href:"signup.php?type=5"});
</script>
<?php } ?>
Why not just use jQuery?
function getUrlVars() {
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
var show = getUrlVars()["show"];
if(show == 1) {
$.colorbox({href:"signup.php?type=5"}).click();
}
Reference: http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html
How about this?
if($_GET['show'] == "1") {
echo '
<script type="text/javascript">
$.colorbox( ... ); // or whatever that triggers the colorbox
</script>
';
}
精彩评论