Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2007-05-14 18:47:58

variaas
Plugin Author
From: Chicago
Registered: 2005-01-16
Posts: 402
Website

Insert into HEAD for admin side plugins

Hey all, so I’ve started playing around with jquery and TXP to create interesting admin side plugins. I have to include scripts and css files on the page, and for right now I’m putting all of this inside of the body tags. I know that they proper way is to put any scripts inside the HEAD tags, but I’m not sure how to do this.

pagetop() opens and closes the head tag and leaves my plugin in the body of the page. Right now, my only thought is to rewrite pagetop() to take an extra parameter for addtional html. Is there another solution available?

Offline

#2 2007-05-15 02:05:35

Mary
Sock Enthusiast
Registered: 2004-06-27
Posts: 6,236

Re: Insert into HEAD for admin side plugins

Sure use an output buffer. Here’s a very barebones example:

function ext_plugin_load()
{
	ob_start('ext_plugin_script');
}

function ext_plugin_script($buffer)
{
	$find = '</head>';
	$replace = n.t.'<script type="text/javascript" src="path/to/js"></script>'.n.$find;

	return str_replace($find, $replace, $buffer);
}

Offline

#3 2007-05-15 03:13:06

variaas
Plugin Author
From: Chicago
Registered: 2005-01-16
Posts: 402
Website

Re: Insert into HEAD for admin side plugins

Very awesome! Thanks again Mary.

I’ve seen ob_start() before but never really understood how it worked. I adapted your example to work with my plugin. Also, if anyone else falls on this page and wants to learn more about output buffers I would recommend starting with this article. I just read through it myself.

Offline

#4 2007-05-15 06:04:08

wet
Developer Emeritus
From: Schoerfling, Austria
Registered: 2005-06-06
Posts: 3,330
Website Mastodon

Re: Insert into HEAD for admin side plugins

variaas wrote:

I know that they proper way is to put any scripts inside the HEAD tags, but I’m not sure how to do this.

This is, IMHO, a myth. It is not required by the W3 recommendations:

The SCRIPT element places a script within a document. This element may appear any number of times in the HEAD or BODY of an HTML document.

In contrary, some rather knowledgable people suggest to put scripts just before the </body> to avoid browser hiccups:

Parsing JavaScript freezes the browser. Therefore put CSS in the head and JavaScript near to the </body> so that it is parsed when the page has been rendered.

Offline

#5 2007-05-15 07:07:54

Mary
Sock Enthusiast
Registered: 2004-06-27
Posts: 6,236

Re: Insert into HEAD for admin side plugins

Yep. style doesn’t though – both style and link elements should only be in the head (though usually browsers will forgive the style element in body).

Offline

#6 2007-06-19 11:07:43

anoke
Archived Plugin Author
Registered: 2006-04-15
Posts: 152

Re: Insert into HEAD for admin side plugins

I used: ( plug test here )

register_callback('nok_jtest', 'article', '');

function nok_jtest($event, $step)
{
    $script = '$(function() { $("body").css("background-color","red"); });';

    $js = <<<EOF
<script language="javascript" type="text/javascript">
<!--
$script 
// -->
</script>
EOF;

    echo $js;

}

Seems to come just before footer. (and yes, i know – having that variable makes no sense. one could just write the jquery between the tags..)

Then again I’m pondering if I should espace that javascript or not.. Maybe the elegant way for non-helloworlds is making it <script type="text/javascript" src=" -url to my js event- "> and having another callback for that. Perhaps version above is ok for oneliners?

Last edited by anoke (2007-06-19 11:13:31)


- When chickens are cold, they roost in trees; when ducks are cold, they plunge into water -

Offline

#7 2007-06-19 20:38:49

Mary
Sock Enthusiast
Registered: 2004-06-27
Posts: 6,236

Re: Insert into HEAD for admin side plugins

Seems to come just before footer.

Yep, that’s what your posted code should do.

Maybe the elegant way for non-helloworlds is making it <script type="text/javascript" src=" -url to my js event- "> and having another callback for that. Perhaps version above is ok for oneliners?

Yes.

Offline

Board footer

Powered by FluxBB