Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Requirements::customScript loads javascript twice


Reply


3 Posts   594 Views

Avatar
cmc

Community Member, 23 Posts

7 September 2013 at 3:07am

How do I prevent Requirements from loading scripts twice?

When using Requirements::customScript to load javascript close to rendering, as described here
http://doc.silverstripe.org/framework/en/reference/requirements the script is added to the end of the HTML twice. This causes any triggers to be fired twice.

I found one forum post which says double loading was fixed in version 2.4
-http://www.silverstripe.org/general-questions/show/13086
This was slightly different as it was loading in the <head> and <header>, rather than twice at the end of the <body>

Another forum post
-http://www.silverstripe.org/template-questions/show/24034
which says a similar issue was fixed in the SS 3.1. There is no SS 3.1 on the downloads page.

On a side note, I find the current search function, which appears to be embedded Google results, frustrating compared to the old forum search. The previous setup gave much more targeted results. One could get either specific forum search results using the internal search, or use Google as a second method.

Avatar
Devlin

Community Member, 217 Posts

7 September 2013 at 3:26am

Dunno what you're actually doing, maybe you've the same script twice in your code.

However you can try the second parameter $uniquenessID of customScript().

http://api.silverstripe.org/3.0/source-class-Requirements.html#88-97

Requirements::customScript("alert('no one');", 'myUniqueID');
Requirements::customScript("alert('me');", 'myUniqueID');

Avatar
cmc

Community Member, 23 Posts

7 September 2013 at 3:34am

Hi Devlin,

Thanks for your quick reply!

I found the undocumented $uniquenessID digging through the source code after I posted. I put a random number in the string and it worked. It's probably smart to use a string that's descriptive of the script so one doesn't accidentally use the same ID twice for different scripts.