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?
2 Answers
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";
6 Comments
user1312079
This seems to go in an infinite loop. :-(
user1312079
When you said add to the link ?lightbox=1 do you mean var url = "www.example.com?lightbox=1";
user1312079
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.
ajax333221
@user1312079 I updated the question to suggest two options on how to append params to the url
user1312079
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?
|
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
user1312079
Can you do this without server side scripting? Thank you.
Rohan
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.