views:

66

answers:

1

I've found that loading the same javascript file twice or more (via PHP) can cause errors in IE (such as: line 1 char 1 code 0). This occurs when a webpage is constructed from nested templates - in this case, CakePHP.

Is there a way to include a file only once?

Perhaps, in CakePHP, it can be done by manipulating the $scripts_for_layout variable? For this to work, scripts would have to be loaded not-inline.

Edit: I'm using 1.2 so the $html->script option is not available. Shame as one of the options is 'once'[true|false]

+2  A: 

Put all your JS files to use in an array. Then, make it unique and use CakePHP script method to include script files.

<?php
//earlier in code... add/edit values in this array
$scripts = array('jquery','wysiwyg','scripts');

//make it unique
$scripts = array_unique($scripts);

//Use CakePHP script method to load multiple external js files:
echo $html->script($scripts);
?>
shamittomar
Sorry, I should have said cakephp 1.2.x - $html->script isn't available. Original Q edited.
Leo
Also, in 1.3 there is a 'once' option available.
Leo
Yes, this works in 1.3. Maybe its time to upgrade.
shamittomar
An upgrade is on the cards, but with twenty-odd sites using my Cake-built CMS, it's not a small task and there is a long list of other things to do first.
Leo
How peculiar - it has started behaving itself. Maybe it was fixed in a maintenance upgrade. As I was going to do something along the lines of what you said, so you get the tick. Thanks.
Leo