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

JavaScript On Page


Reply


5 Posts   849 Views

Avatar
davede

Community Member, 24 Posts

23 August 2011 at 5:32am

Edited: 23/08/2011 5:33am

Hi all,

Been looking around the forums for a solution but keep hitting dead ends and threads that aren't very clear.

I'd like to embed the following code on our site:

<script src="http://player.ooyala.com/player.js?height=360&amp;deepLinkEmbedCode=Y0NGRjMjplERX85mydGU70V1qVvFPvbX&amp;video_pcode=oza2w6q8gX9WSkRx13bskffWIuyf&amp;autoplay=1&amp;embedCode=Y0NGRjMjplERX85mydGU70V1qVvFPvbX&amp;width=640"/>

It is an embed code for a video that seems to use javascript instead of an iframe.

I created a separate TextareaField for the CMS and made it the data type HTMLText. Then I put the place holder in the template.

However when I publish the page all the script code gets escaped like so:

&lt;script src=&quot;http://player.ooyala.com/player.js?height=360&amp;amp;deepLinkEmbedCode=Y0NGRjMjplERX85mydGU70V1qVvFPvbX&amp;amp;video_pcode=oza2w6q8gX9WSkRx13bskffWIuyf&amp;amp;autoplay=1&amp;amp;embedCode=Y0NGRjMjplERX85mydGU70V1qVvFPvbX&amp;amp;width=640&quot;/&gt;

How can we embed the code on the page?

Please note creating a separate page type every time we'd like to embed a video is not feasible.

Any help would be much appreciated.

Avatar
MagicUK

Community Member, 60 Posts

23 August 2011 at 8:30am

To load javascript into a page you have to add the requirement into the controller for the page you want it to be on:

class Page_Controller extends ContentController {

   
   public static $allowed_actions = array (
   );

   public function init() {
      parent::init();

      Requirements::javascript("js/someJavaScript.js");
   }
}

Avatar
davede

Community Member, 24 Posts

23 August 2011 at 8:51am

Hi Magic UK,

Thanks for the reply :)

Perhaps I should have been a bit clearer.

This is something that we need to do on a recurring basis.

We'd like to embed videos in content.

However the same issue extends to:
- Google adsense code
- Google widgets
- any other embed code that uses javascript

We would really like a simple solution to simply embed any JS into the content of a page.

Thanks in advance.

Avatar
Willr

Forum Moderator, 5513 Posts

23 August 2011 at 8:01pm

@davede - in your example you would need to use $FieldName.RAW to stop any escaping. This does pose a slight security issue if you allow outsiders into your CMS which is usually very rare.

Another popular method is using shortcodes - http://www.ssbits.com/tutorials/2010/2-4-using-short-codes-to-embed-a-youtube-video/

Avatar
davede

Community Member, 24 Posts

23 August 2011 at 11:20pm

Hi Will,

You're a good man thanks for all the help.

We changed the datatype to Text and put the RAW method on the end of the placeholder.

That seems to work fine and now we can put whatever code we want into the page.

I wasn't familiar with short codes thanks for the info. We may implement it in the future however for now we may just stick to the quick and dirty paste any code you want way.