SimplePortal
Customization => Custom Coding => Topic started by: locutusweb on May 11, 2011, 08:54:51 AM
-
What's the best way to add a line to a javascript file to the SMF header (between the <head> tags) for a custom block/page?
-
You should use PHP type in blocks and in pages. And body like:
global $context;
$context['html_headers'] .= 'JavaScript';
echo 'Content';
-
Something like this?
global $context;
$context['html_headers'] .= 'http://www.mydomain.com/Themes/default/scripts/myscript.js';
echo 'Content';
-
The problem is, though, that the SimplePortal block content is not evaluated until AFTER the page header has already been sent.
Have you run into problems with this?
Barring that, you can simply load the javascript while you load the page/block, using a regular <javascript... tag in an HTML or php block.
Do you really need it to go in the page head?
-
Like this:
global $context;
$context['html_headers'] .= '
<script type="text/javascript" src="http://www.mydomain.com/Themes/default/scripts/myscript.js"></script>';
echo 'Content';
-
Does that work?
The custom php blocks/pages are eval'd AFTER the headers are emitted, so can altering $context['html_headers'] make any difference to how the html headers are rendered?
-
Oh, you're right, Angelina. Should be a side effect of flu. :ill:
As a result, locutusweb, should add JavaScript files manually to SMF.
A custom function to see whether we want a block -- add it before ?> in index.template.php.
function in_loaded_blocks($block_id)
{
global $context;
static $loaded_blocks = array();
if (isset($loaded_blocks[$block_id]))
return true;
foreach ($context['SPortal']['blocks'] as $side => $blocks)
{
foreach ($blocks as $block)
{
if ($block_id == $block['id'])
{
$loaded_blocks[$block['id']] = $block['id'];
return true;
}
}
}
return false;
}
Usage:
if (in_loaded_blocks(5))
echo 'Codes...';
Or for a page...
if (isset($context['SPortal']['page']) && $context['SPortal']['page'] == 5)
echo 'Codes...';