Poll callback

The callback feature allows you to define a JavaScript function that will be called when the poll loads, when viewing results and after a poll vote has been registered.

This function can do anything from simply showing a “Thank you for voting!” message to making an AJAX request to update something on your system.

To use this feature you simply create a variable called pd_callback in a script tag. Assign it a function. Then when a vote is registered this function will be invoked.

The function will return a JSON string that can be parsed for information on the type of vote request was made.

  • When poll is loaded (on page load and when user clicks ‘return to poll’ link), it will return¬†
    '{"id":123456,"result":"load"}'
  • If a vote was registered, it will return '{"id":123456,"answer":[54321],"other_answer":"","result":"registered"}'
  • If a vote was blocked, it will return
    '{"id":123456,"result":"already-registered"}'
  • If a view results request, it will return
    '{"id":123456,"result":"view-results"}'

Here is an example of it in action…

<script type="text/javascript" charset="utf-8">
var pd_callback = function( json ) {

	var obj = jQuery.parseJSON( json );

	if ( obj.result == 'load' )
		alert('Poll' + obj.id + ' has loaded!');
	else if ( obj.result == 'registered' )
		alert('Thank you for voting!');
	else if ( obj.result == 'already-registered' )
		alert('You already voted!');
	else if ( obj.result == 'view-results' )
		alert('Check out these results!');
};
</script>
<script type="text/javascript" charset="utf-8" src="http://static.polldaddy.com/p/4035240.js"></script>

* It’s important to place the script tag, that contains the pd_callback variable, before the script tag that references the poll JavaScript file.

If you have more than one poll on a page you may want to have a unique callback for each poll. In this case, use a variable like pd_callback_[poll id]. So in the example above it will now look like this…

<script type="text/javascript" charset="utf-8">
var pd_callback_4035240 = function(){
	alert('Thank you for voting!');
};
</script>
<script type="text/javascript" charset="utf-8" src="http://static.polldaddy.com/p/4035240.js"></script>
Polldaddy Tweets
%d bloggers like this: