Jump to:

23377 Posts in 18296 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » Script content getting stripped

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

Page: 1
Go to End
Author Topic: 1328 Views
  • steve_nyhof
    Avatar
    Community Member
    224 Posts

    Script content getting stripped Link to this post

    I have been doing quite fine with this. I have setup my LeftAndMain.php file by first adding in the script[scr|... to the end of the valid_elemants

    I have also try 'valid_elements' => "*

    • ", star...star

      But SS keeps stripping half of this code for google website optimizer...
      This is what I insert...

      <script type="text/javascript">
      if(typeof(_gat)!='object')document.write('<sc'+'ript src="http'+
      (document.location.protocol=='https:'?'s://ssl':'://www')+
      '.google-analytics.com/ga.js"></sc'+'ript>')</script>
      <script type="text/javascript">
      try {
      var gwoTracker=_gat._getTracker("UA-11120272-2");
      gwoTracker._trackPageview("/2027641438/test");
      }catch(err){}</script>

      SS strips it to this...

      <script type="text/javascript"><!--
      if(typeof(_gat)!='object')document.write('<sc'+'ript src="http'+
      (document.location.protocol=='https:'?'s://ssl':'://www')+
      '.google-analytics.com/ga.js"></sc'+'ript>')
      // --></script>
      <script type="text/javascript"></script>

      Removing the second script - try {...

      Anyone have some ideas? I want this done on the page, not on the template.

      I was wondering if I could add a second custom metadata field, and then a tag $CustomMeta2 right before the /body tag ???

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: Script content getting stripped Link to this post

    Your second approach would work, but this seems like the third post I've replied to you saying you should move your JS into .js files then include them in templates instead of finding so many damned ways to hack around and not do it that way.

    Seriously, I just did google analytics with js templates and it was a breeze to switch out code tracker codes/HTTPS, etc. If you'd like to know how to do this the easy way, tell me you'll stop this hackery!

  • steve_nyhof
    Avatar
    Community Member
    224 Posts

    Re: Script content getting stripped Link to this post

    I promise to stop the hackery! But I just am not a programmer and need to get this project off the ground.

    I was able to add a field into my metadata tab for a body script which works great and I'm moving forward. Now I do not need to add the head and body scripts into the editor.

    One question I have is how to add spaces to my CustomBodyTags name... Custom Body Tags so it shows up nice on the Metadata tab area. Also, how can I make the field 5 rows high to look like the other Meta tag fields?

    The reason I do not want to make templates is because I want to make many split pages, each require a different script code/id.

    public static $db = array(
    'CustomBodyTags' => 'HTMLText'
    );

    public static $has_one = array(
    "HeaderImage" => "Image"
    );

    function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab("Root.Content.Main", new ImageField("HeaderImage"));
    $fields->addFieldToTab('Root.Content.Metadata', new TextField('CustomBodyTags'));
    return $fields;
    }

    }

  • steve_nyhof
    Avatar
    Community Member
    224 Posts

    Re: Script content getting stripped Link to this post

    I got this figured out now too...

    $fields->addFieldToTab("Root.Content.Metadata", new TextareaField("CustomBodyTags", "Custom Body Tags"));

    Added the TextareaField and the , "Custom Body Tags" to get my spaces.

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: Script content getting stripped Link to this post

    Bravo, sir! Now here is how you do it with JS Templates:

    1. Create a place to store your codes with your Pages, similar to what you did above, in Page.php

    public static $db = array(
       'GoogleTrackingCode' => 'Varchar(255)'
    );

    function getCMSFields() {
       $fields = parent::getCMSFields();
       $fields->addFieldToTab('Root.Content.Metadata', new TextField('GoogleTrackingCode'));
       return $fields;
    }

    2. Create a JS Template file, for this example: mysite/templates/javascript/ga.template.js

    try { var pageTracker = _gat._getTracker("{$TrackCode}"); pageTracker._trackPageview(); } catch(err) {}

    3. User Requirements to put your custom variables in your template, in Page.php

    public function init() {
       parent::init();
       if( isset($_SERVER['SSL']) || (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ) {
          Requirements::javascript('https://ssl.google-analytics.com/ga.js');
       } else {
          Requirements::javascript('http://www.google-analytics.com/ga.js');
       }

       $jsData = array('TrackCode' => $this->data()->GoogleTrackingCode );
       Requirements::javascriptTemplate('mysite/templates/javascript/ga.template.js',$jsData);
    }

    Now on a per page basis you can specify your tracking code. It isn't a 100% solution but it should be a good start to storing only what you need and using templates to consolidate code. Of course what you did is also perfectly OK, but I just want to reinforce the methods that exist as the creators intended.

    1328 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.