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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Silverstripe CMS Including Javascript


Go to End


5 Posts   1604 Views

Avatar
Scott Farmer

Community Member, 49 Posts

9 October 2014 at 4:32pm

Hi there,

I have CMS admin page form (from a gridfield, editing item) with a dropdown that calls a AJAX script to check some conditions and post back a message warning user that change might cause some issues. I have this part all working, but have to reload page after clicking edit to get javascript file loaded.

Have placed this line in _config.php
LeftAndMain::require_javascript('mysite/javascript/my_scripts.js');

This doesn't get loaded after you click edit from main gridfield table list.

What is the best way to hook javascript into the cms to use in forms?

Thanks
Regards
Scott

Avatar
kinglozzer

Community Member, 187 Posts

9 October 2014 at 9:10pm

Hi Scott,

If your javascript is specific to that one edit form, it might be a better idea to load it in the getCMSFields() method with a simple Requirements::javascript() call.

Hope this helps

Loz

Avatar
Scott Farmer

Community Member, 49 Posts

9 October 2014 at 10:10pm

Hi Loz,

Tried that also with the same result.

Thanks
Regards
Scott

Avatar
kinglozzer

Community Member, 187 Posts

10 October 2014 at 12:47am

Hi Scott,

Is the file definitely not being loaded? It should be included in an X-Include-JS response header when you enter the "edit" view.

If it is being loaded, but is’t executing as you’d expect, you might need to use Entwine to get it to work.

Loz

Avatar
Scott Farmer

Community Member, 49 Posts

10 October 2014 at 11:31am

Hi Loz,

Thanks for your help. I had a closer look using firebug and could see JS file getting loaded, but required a browser refresh for JS code to work. So for whatever reason, my javascript code was getting ignored.

Entwine did the trick! Cheers for pointing that out.

For others that have this issue, an example javascript code is:
$('#Form_ItemEditForm_DPSPaymentConfirmed').entwine({
onclick: function(){ alert("Entwine Button clicked"); }
});

Thanks
Regards
Scott