+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Managing script content in head tag... best practice?

Managing script content in head tag... best practice?

5 posts, 0 answered
  1. Jess Askey
    Jess Askey avatar
    20 posts
    Registered:
    25 Feb 2010
    20 Jul 2010
    Link to this post
    Hi all..

    Im building a fairly complex site in Sitefinity 3.7 (moving to 4.0 ASAP) and I have several situations where I want to have the ability to add <script> tags in the head of various pages created in Sitefinity. I have tried putting a ContentPlaceHolder in the head section so that I could drag a 'Generic Content' control in when needed and add the necessary tags. The problem with this was that the sitefinity admin GUI didn't seem to support this as it got really confused and basically made the page editor un-usable.

    I thought about making my own control that was similar to the Generic Content control but would try and jam it's content into the head with a method like Page.Header.Controls.Add(new Literal("<script></script")). I can already see that this might not work and I didn't want to spend a bunch of time on this if there was a better way.

    Any ideas?

    Will Sitefinity 4.0 have any specific features to address this issue? I haven't seen anything in the docs so far, so I figured I would ask.

    thank you

    jess
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    20 Jul 2010
    Link to this post
    Hi Jess Askey,

    You can use the built-in JsFileLink or CssFileLink controls. You can create a user control and expose their properties so that you will be able to set them through Sitefinity's control designer. Another option is crating a custom control as this one shown below

    protected override void CreateChildControls()
     {
        
       if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "Block")
       {
         var scriptText = new StringBuilder();
         scriptText.Append("<script type=\"text/javascript\"> function click() {");
         scriptText.Append("Form1.Message.value='Hello'} </");
         scriptText.Append("script>");
         Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Block", scriptText.ToString());
       }
     }


    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Aaron
    Aaron avatar
    12 posts
    Registered:
    11 Oct 2011
    11 Oct 2011
    Link to this post
    I need help with where this needs to go in a Sitefinity site. My background is with HTML, XHTML and CSS, I understand the basics of Javascript, ASP.net, PHP and Jquery but I am a designer and not a programmer so while I know how most pieces and parts come together I do not know the syntax for the higher languages and thus do not write that code. I have a piece of javascript from a mobile website that was built internally that I need to add to the site globally for a mobile site redirect and of course if this were built in HTML or even another CMS like Wordpress I would simply drop this bit of code into the <head> area and be done but I’m gathering from my searches that Sitefinity does not work that way and until I can get to some training on the system I’m at a bit of a loss as to where to put this code.

    <script type="text/javascript">
    /*
     *     MoFuse Premium API
     *    Mobile Detect & Redirect
    */
    var mfp_url='http://m.incharge.org';
    var mfp_host_name=document.location.hostname;var mfp_request_uri=document.location.pathname;var mfp_no_mobile=location.search;var mfp_cookie=document.cookie;function mf_detect(){var mfp_ua=navigator.userAgent.toLowerCase();var mfp_devices=['vnd.wap.xhtml+xml','sony','symbian','nokia','samsung','mobile','windows ce','epoc','opera mini','nitro','j2me','midp-','cldc-','netfront','mot','up.browser','up.link','audiovox','blackberry','ericsson','panasonic','philips','sanyo','sharp','sie-','portalmmm','blazer','avantgo','danger','palm','series60','palmsource','pocketpc','smartphone','rover','ipaq','au-mic','alcatel','ericy','vodafone','wap1','wap2','teleca','playstation','lge','lg-','iphone','android','htc','dream','webos','bolt','nintendo'];for(var i in mfp_devices){if(mfp_ua.indexOf(mfp_devices[i])!=-1){return true}}}if(mfp_no_mobile!='?nomobile=1'&&mfp_cookie.indexOf('mfp_no_mobile')==-1){mfp_is_mobile=mf_detect();if(mfp_is_mobile){window.location=mfp_url+"?h="+mfp_host_name+"&r="+mfp_request_uri}}else{if(mfp_cookie.indexOf('mfp_no_mobile')!=-1){}else{mfp_cookie_expires=new Date();mfp_cookie_expires.setTime(mfp_cookie_expires.getTime()+1000*60*60*24);document.cookie="mfp_no_mobile=1; expires="+mfp_cookie_expires.toGMTString()+"; path=/;"}}
    </script>
    <noscript>
    <meta http-equiv="refresh" content="0; URL=http://m.incharge.org">
    </noscript>


    Any help you could provide me would be greatly appreciated
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    12 Oct 2011
    Link to this post
    Hello Aaron,

    One of the options is using jQuery and append().Another option is using this.Page.Header.Controls.Add(yourcontrol).


    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Aaron
    Aaron avatar
    12 posts
    Registered:
    11 Oct 2011
    12 Oct 2011
    Link to this post
    Perhaps in english please :) Where might I find a step by step on this?

    Thanks!
Register for webinar
5 posts, 0 answered