Last active
December 30, 2015 11:19
-
-
Save ziogaschr/7821770 to your computer and use it in GitHub Desktop.
JS: Force showing a popup video to user before adding item to cart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Force showing a popup video to user before adding item to cart | |
* Use Case: Stokke Tripp-Trapp | |
*/ | |
$(document).ready(function () { | |
var success_hash = 'tripptrapp-success'; | |
// check if this is the success page | |
if(window.location.hash.substring(1) == success_hash) { | |
// set a success cookie for a day so as we prevent showing the video | |
// to the same user on the same day | |
setCookie('TRIPPTRAPP_SUCCESS_ACHIEVED', true, 1); | |
// call parent window addToCart() | |
window.opener.addToCart(); | |
// close this popup | |
window.close(); | |
} | |
// on cart button click | |
$("#cart_button").click(function (event) { | |
// if user hasn't seen the video | |
if (getCookie('TRIPPTRAPP_SUCCESS_ACHIEVED') !== 'true'){ | |
// show the video | |
openstokke(); | |
// prevent item from being added in the cart | |
event.preventDefault(); | |
} | |
}); | |
function openstokke() { | |
// be carefull when changing RETURNSUCCESS, | |
// so as to not remove '%23tripptrapp-success'. '%23' => # | |
var newWin = | |
window.open('http://media.stokke.com/tripptrapp/movie.aspx?' | |
+ 'lang=el' | |
+ '&RETURNSUCCESS=MY_RETURN_URL%23' + success_hash, null, | |
'fullscreen=no,titlebar=no,status=no,toolbar=no,menubar=no,location=no,scrollbars=no') | |
}; | |
function setCookie(c_name, value, exdays) { | |
var exdate = new Date(); | |
exdate.setDate(exdate.getDate() + exdays); | |
var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString()); | |
document.cookie = c_name + "=" + c_value; | |
}; | |
function getCookie(c_name) { | |
var i, x, y, ARRcookies = document.cookie.split(";"); | |
for (i = 0; i < ARRcookies.length; i++) { | |
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("=")); | |
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1); | |
x = x.replace(/^\s+|\s+$/g, ""); | |
if (x == c_name) { | |
return unescape(y); | |
} | |
} | |
}; | |
}); | |
// this function has been added to the global scope | |
// so as the popup window can access it | |
function addToCart() { | |
$("#cart_button").trigger('click'); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment