I am using a cookie in conjunction with Youtube subscriber code.
Using the cookie code from this posting, Create, read, and erase cookies with jQuery
I add createCookie when Youtube returns a subscribe event, and eraseCookie when an unsubscriber event, followed by th approproate redirect. Since this code is not jquery, I place this code outside the jquery ready function.
function onYtEvent(payload) {
console.log(payload);
if (payload.eventType == 'subscribe') {
// Add code to handle subscribe event.
createCookie('subscribed','yes',30);
location.hash = '#mainpage';
} else if (payload.eventType == 'unsubscribe') {
// Add code to handle unsubscribe event.
eraseCookie('subscribed');
location.hash = '#subscribepage';
}
if (window.console) { // for debugging only
window.console.log('YT event: ', payload);
}
}
then inside the jquery ready function, I add the readCookie function
if (readCookie('subscribed') === 'yes') {
location.hash = '#mainpage';
} else {
location.hash = '#subscribepage';
}
I am using JQM to handle my page redirections.
The goal I accomplished is when the page first loads will read the subscribed cookie, if present, I know the user has subscribed, and redirect to the mainpage.
If the cookie does not exist, will redirect to the subscriber page showing the youtube subscriber button.
Therefore, the user only needs to press the subscribe button once.
Using the YouTube API require additional authentication by the user, and subject to quota limits. I wanted a simpler solution not subject to these limits, so cookies are use as described above.
You can check my example at http://recipes.quickminutemeals.com