function addtoany_page_alter in AddToAny Share Buttons 7.3
Same name and namespace in other branches
- 5.0 addtoany.module \addtoany_page_alter()
- 7.4 addtoany.module \addtoany_page_alter()
- 7 addtoany.module \addtoany_page_alter()
Implementation of hook_page_alter().
Return value
String containing JavaScript code for asynchronous loading of AddToAny's external script.
File
- ./
addtoany.module, line 130 - Stand alone module file to handle AddToAny buttons integration
Code
function addtoany_page_alter(&$page) {
global $_addtoany_init, $_addtoany_targets;
// Only output script when AddToAny is used
if (!$_addtoany_init) {
return;
}
$script_url = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https://static.addtoany.com/menu/page.js' : 'http://static.addtoany.com/menu/page.js';
$javascript_header = "var a2a_config=a2a_config||{}," . "da2a={done:false," . "html_done:false," . "script_ready:false," . "script_load:function(){" . "var a=document.createElement('script')," . "s=document.getElementsByTagName('script')[0];" . "a.type='text/javascript';a.async=true;" . "a.src='" . $script_url . "';" . "s.parentNode.insertBefore(a,s);" . "da2a.script_load=function(){};" . "}," . "script_onready:function(){" . "if(a2a.type=='page'){" . "da2a.script_ready=true;" . "if(da2a.html_done)da2a.init();" . "}" . "}," . "init:function(){" . "for(var i=0,el,target,targets=da2a.targets,length=targets.length;i<length;i++){" . "el=document.getElementById('da2a_'+(i+1));" . "target=targets[i];" . "a2a_config.linkname=target.title;" . "a2a_config.linkurl=target.url;" . "if(el)a2a.init('page',{target:el});da2a.done=true;" . "}" . "}" . "};" . "a2a_config.tracking_callback=['ready',da2a.script_onready];" . variable_get('addtoany_additional_js', '');
$javascript_footer = "da2a.targets=[" . implode(",", $_addtoany_targets) . "];\n" . "da2a.html_done=true;" . "if(da2a.script_ready&&!da2a.done)da2a.init();" . "da2a.script_load();";
// Load external script if not already called with the first AddToAny instance. Fixes issues where first instance code is processed internally but without actual code output
drupal_add_js($javascript_header, array(
'type' => 'inline',
'scope' => 'header',
));
drupal_add_js($javascript_footer, array(
'type' => 'inline',
'scope' => 'footer',
));
}