1

I have a web page with a jQuery lightbox that opens automatically on page load. In the lightbox I have implemented the Facebook Like button (this is a 2 click solution for Facebook Like button in email). Now, when a Facebook user visits my web page by clicking on the "Liked" URL on Facebook I want to turn off the lightbox. I don't want to have to create two different pages (one with lightbox turned on and another turned off) and I think I can avoid this by adding a parameter to the URL with Javascript then turn the lightbox on/off based on that parameter. Is this a safe way to do this? How would I do this?

user1312079
  • 79
  • 1
  • 7

2 Answers2

1

You can get URL params with this JS function that I found here:

function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}

Then *add use ?lightbox=1 e.g www.example.com?lightbox=1 and read it with the above function:

if(getUrlVars()["lightbox"]=="1"){
    //...
}

*to add parameters you can either:

  • Change the a link href attribute element.href = "http://www.newURL.com?param=1"; and wait for the user to click them
  • Redirect to the new location window.location.href = "http://www.newURL.com?param=1";
ajax333221
  • 11,436
  • 16
  • 61
  • 95
  • This seems to go in an infinite loop. :-( – user1312079 Apr 20 '12 at 18:44
  • When you said add to the link ?lightbox=1 do you mean var url = "www.example.com?lightbox=1"; – user1312079 Apr 20 '12 at 21:45
  • The function only grabs the url param but not append to it ?lighbox=1. I'm trying to append to the url so that I can turn the lighbox on and off based on the the new param. – user1312079 Apr 20 '12 at 21:58
  • @user1312079 I updated the question to suggest two options on how to append params to the url – ajax333221 Apr 21 '12 at 00:30
  • Got it to work by appending param to facebook iframe like button. With XFBML facebook does not accept param ?lightbox=1. Any ideas? – user1312079 Apr 22 '12 at 04:39
  • Found what I was looking for here: http://stackoverflow.com/questions/3149455/facebook-like-href-url-vs-canonical-url And here: http://developers.facebook.com/docs/reference/plugins/like/ Thanks for your help! – user1312079 Apr 23 '12 at 18:30
0

I believe this is the rough code you're after:

<script type="text/javascript">
    document.location="http://example.com/index.php?noLightBox=1";
</script>

Then on index.php, you'd want something such as:

<?php 
function isset_or(&$check, $alternate = NULL) 
{ 
    return (isset($check)) ? (empty($check) ? $alternate : $check) : $alternate; 
} 

function getGETPOST($var) 
{ 
       return isset_or($_GET[$var],isset_or($_POST[$var],"Empty")); 
} 
?>

Example:

if(getGETPOST('noLightBox') != 1) {
// Code to display the light box comes here.
}
// Else normal page code

Hope this was helpful! Source: http://php.net/manual/en/function.isset.php & W3School's tutorials on PHP

Rohan Durve
  • 340
  • 2
  • 14
  • Can you do this without server side scripting? Thank you. – user1312079 Apr 20 '12 at 03:38
  • On the first page it is local client sidded JavaScirpt, if you want to not have the other end in PHP here's a way to do it in JavaScript. http://stackoverflow.com/questions/1961069/getting-value-get-or-post-variable-using-javascript It's most tedious tho. – Rohan Durve Apr 20 '12 at 03:46