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?

0

2 Answers 2

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";
Sign up to request clarification or add additional context in comments.

6 Comments

This seems to go in an infinite loop. :-(
When you said add to the link ?lightbox=1 do you mean var url = "www.example.com?lightbox=1";
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 I updated the question to suggest two options on how to append params to the url
Got it to work by appending param to facebook iframe like button. <iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.example.com%2Fexample.html?lightbox=1"></iframe> With XFBML facebook does not accept param ?lightbox=1. <fb:like href="example.com/example.html?lightbox=1" send="true" width="450" show_faces="true"></fb:like> Any ideas?
|
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

2 Comments

Can you do this without server side scripting? Thank you.
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. stackoverflow.com/questions/1961069/… It's most tedious tho.

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.