SimplePortal

Customization => Custom Coding => Topic started by: locutusweb on May 11, 2011, 08:54:51 AM

Title: add javascript to the SMF header
Post 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?
Title: Re: add javascript to the SMF header
Post by: grafitus on May 12, 2011, 05:05:58 AM
You should use PHP type in blocks and in pages. And body like:
Code: [Select]
global $context;

$context['html_headers'] .= 'JavaScript';

echo 'Content';
Title: Re: add javascript to the SMF header
Post by: locutusweb on May 12, 2011, 05:12:27 AM
Something like this?
Code: [Select]
global $context;

$context['html_headers'] .= 'http://www.mydomain.com/Themes/default/scripts/myscript.js';

echo 'Content';
Title: Re: add javascript to the SMF header
Post by: AngelinaBelle on May 12, 2011, 09:45:50 AM
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?
Title: Re: add javascript to the SMF header
Post by: grafitus on May 12, 2011, 10:04:15 AM
Like this:
Code: [Select]
global $context;

$context['html_headers'] .= '
<script type="text/javascript" src="http://www.mydomain.com/Themes/default/scripts/myscript.js"></script>';

echo 'Content';
Title: Re: add javascript to the SMF header
Post by: AngelinaBelle on May 12, 2011, 11:38:49 AM
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?
Title: Re: add javascript to the SMF header
Post by: grafitus on May 12, 2011, 03:58:12 PM
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.
Code: [Select]
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:
Code: [Select]
if (in_loaded_blocks(5))
echo 'Codes...';

Or for a page...
Code: [Select]
if (isset($context['SPortal']['page']) && $context['SPortal']['page'] == 5)
echo 'Codes...';
SimplePortal 2.3.8 © 2008-2024, SimplePortal