Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jay 49 posts 211 karma points
    Jan 09, 2015 @ 11:49
    Jay
    0

    How To Include Scripts Via Umbraco

    Hi Umbracians,

    Depending on whether I am publishing an Umbraco site to my local dev machine or the client's test/production machine, I want to include a different bit of Google Tag Manager code.

    Is there a way to conditionally include a specific script?

    Umbraco has relieved nearly all my page editing nightmares, save for this one!

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Jan 09, 2015 @ 11:53
    Jan Skovgaard
    1

    Hi Jay

    A common approach is to usually use a textbox multiple to save the code snippet (textarea) and then you can render the code using a macro, which means it will of course render different codes on different environments.

    You can have to boxes one for scripts that need to go in the section of your page and then one for rendering those that need to be placed before .

    That way your client has maximum freedom (and responsibility of course) and you don't risk messing things up yourself.

    Hope this makes sense.

    /Jan

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jan 09, 2015 @ 12:04
    Dennis Aaen
    0

    Hi Jay,

    What you can do it is by creating a field in your Umbraco where the use can be able to write the whole script, or just the part that is a bit different bit.

    Let me give you an example, normally I creates a tab on the homepage node called site settings in this tab I add a field called Google analytics id, this field has the type of textstring. Then user can add the google analytics key in it .

    Then in the master template I add the script block for the google analytics, where the only thing I get from Umbraco is the google analytics id field. 

    If you want to give your user a field in Umbraco where they can paste whole scripts in, then I have good experience using the multiple textbox.

    Hope this helps, and make sense. If you have other questions keep asking.

    /Dennis

  • Jay 49 posts 211 karma points
    Jan 09, 2015 @ 12:08
    Jay
    0

    Great suggestions, thanks a lot!

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Jan 09, 2015 @ 13:25
    Steve Morgan
    0

    Just to chime in.. I usually use a SiteSettings node with a footerJsCss text field to add sitewide code ( e.g. Google Analytics). Don't forget to use  @Html.Raw if you're going to include <script> tags etc  - I use the following in my Master template but you could of course do this on a page by page basis.

         @* Get site settings node *@
        var siteSettings = Model.Content.AncestorOrSelf(1).Siblings().Where("DocumentTypeAlias = \"SiteSettings\"").First();
    @Html.Raw(siteSettings.GetPropertyValue("footerJsCss"))
  • Mahesh 20 posts 90 karma points
    Feb 25, 2020 @ 15:57
    Mahesh
    0

    Hi All,

    In umbraco 6.2.5 verstion I am able add the script tags for each and every page.

    enter image description here

    from above image you can find the scripts tab.

    but in umbraco 7.11.1 I could not able to see that feature. Is it possible to enable the feature if we can how it is possible.

    thank you.

  • Eric Wilkinson 21 posts 91 karma points
    Feb 25, 2020 @ 21:35
    Eric Wilkinson
    0

    @Mahesh,

    In Umbraco 7, you would still go to your base page Document Type under the Settings section, and then add a Tab called Scripts, and add the field(s) for your injected JS/CSS there using Textarea DataTypes.

    Then in your Partial View for your master template (where youre and tags are) you would render the content basically the same way as in Umbraco v6, except using the typed content version of your Model rather than accessing the property dynamically.

    @Html.Raw(Model.Current.GetPropertyValue<string>("footerJsCss"))
    
  • Mahesh 20 posts 90 karma points
    Feb 26, 2020 @ 14:53
    Mahesh
    0

    Hi Eric Wilkinson,

    Thank you for your response.I have added the script tab as per you suggestion.

    enter image description here

    From above image i have added the script tab and one property as textarea to add the script tags. But the script tag is displaying for all modules under home. I need to add the different scripts to the different pages.

    enter image description here

    ex: I need to add one script for innovation for good page and i need to add some other script tag to community page.

    please give me any suggestion. Thank you.

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Feb 26, 2020 @ 16:16
    Steve Morgan
    1

    Hi Mahesh,

    This will do exactly what you need. It's changed the document type (think of this as a blueprint for the types of content that a page can have) but each individual page using that doc type has it's own content data.

    So just because the field appears on each page it stores its own copy of the data - does that make sense?

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Feb 26, 2020 @ 16:20
    Steve Morgan
    1

    What it will require is for you either to add that script to every page that needs it or to change the logic for it so it finds the nearest script from it's parents (which is what I suspect you want).

    If so, change the code Eric suggested to:

    @Html.Raw(Model.Current.GetPropertyValue<string>("footerJsCss", true))
    

    That true flag will search recursively up the tree until it finds a script.

  • Mahesh 20 posts 90 karma points
    Feb 28, 2020 @ 08:51
    Mahesh
    0

    Hi Steve Morgan,

    I have used below line of code to get the values from Umbraco and it is working fine for all pages.

    var script = Model.Content.GetProperty(customScript).Value;

    Thank you.

Please Sign in or register to post replies

Write your reply to:

Draft