Copied to clipboard

Flag this post as spam?

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


  • Warren Buckley 2106 posts 4836 karma points MVP ∞ admin hq c-trib
    Sep 28, 2010 @ 22:47
    Warren Buckley
    0

    RegisterStyleSheetFile & RegisterJavaScriptFile without runat=server on head tag

    Hello All,
    I am currently working on an XSLT file that will be part of a package.

    However when ever someone uses the macro on a page, be it via RTE or on a template, I would like it to add the necessary CSS & JS files to the page, obviously ideally in the <head> tag.

    I have done the following in my XSLT file

      <xsl:template match="/">
        
        <!-- Register CSS & JS -->
        <xsl:value-of select="umbraco.library:RegisterStyleSheetFile('uTubeCSS', '/css/YouTube.css')"/>
        <xsl:value-of select="umbraco.library:RegisterJavaScriptFile('jQueryGoogleCDN', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js')"/>
        <xsl:value-of select="umbraco.library:RegisterJavaScriptFile('uTubeJS', '/scripts/YouTube.js')"/>

         <!-- Rest of code -->

    I have checked the page and they have not been added to the page, unless I specifically add runat="server" to the <head> tag.

    Couple of questions then:

    1. As a package developer I cannot ensure that everyone is using the runat="server" on the <head> tag.
      Is there still a way to add the CSS and JS files into the page?
    2. Also am I using this correctly in the XSLT?
      Should they be in variables or params instead of just plain old value-of 's?

    Thanks,
    Warren :)

  • Casey Neehouse 1339 posts 483 karma points MVP 2x admin
    Sep 28, 2010 @ 23:07
    Casey Neehouse
    0

    What about  processing the templates and adding it where <head> is found without the runat="server"?

     

     

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Sep 28, 2010 @ 23:45
    Aaron Powell
    0

    What version of Umbraco are you finding this error in? Looking at the code for 4.5 it should handle not having runat="server" in the head.

    Alternatively you can just put the CSS/ JavaScript files out in-line on the page, this may mean you will have the same files registered multiple times (but without a runat="server" in the head you're going to hit this problem anyway)

  • Warren Buckley 2106 posts 4836 karma points MVP ∞ admin hq c-trib
    Sep 29, 2010 @ 09:26
    Warren Buckley
    0

    @Casey I am not sure what you mean by processing the templates?

    @Aaron I am running version 4.5.2 and this xslt is on an alt template so no currentPage context if that has anything to do with the problem.
    Ideally don't want to add the CSS and JS directly in the XSLT, as if the macro is used multiple times on a page then we will have duplicate calls to CSS and JS which seems unecessary.

    Look forward to see how this can be resolved.

    Cheers,
    Warren :)

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Sep 29, 2010 @ 09:45
    Matt Brailsford
    2

    Hey Warren,

    As you are reliant on JS anyway, why not append them to the head section using javascript? There is a good example of this in the backfire package where it registers a CSS file at the point of initialisation.

    Matt

  • Warren Buckley 2106 posts 4836 karma points MVP ∞ admin hq c-trib
    Sep 29, 2010 @ 09:48
    Warren Buckley
    0

    Heya Matt,
    I will take a look at that. However I will still need to register that JS file in the XSLT and surely wouldn't that repeat the addition of the extra JS and CSS files needed?

    Warren :)

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Sep 29, 2010 @ 09:51
    Lee Kelleher
    0

    From looking at the source of "RegisterJavaScriptFile", I would have thought that the JavaScript would be registered on the page somewhere - not necessarily in the <head> - but at least somewhere?

    I think Casey might mean processing the template using a HttpModule?  You could use RegEx to check for specific tags (could be the YouTube macro, or some HTML comment, or whatever) ... then inject the <script> tags into the <head>.  But this isn't a very elegant solution, reminds me too much of PHP kiddie-script hacking! ;-)

    IMO, the best way to handle this for Umbraco is to insert a Client Dependency control into the <head> at package install - like a Runway module. Then if the user decides to remove it, then they should be skilled enough to manually include the CSS/JS where needed? (or is that wishful thinking?)

    Cheers, Lee.

  • Ian Smedley 97 posts 192 karma points
    Sep 29, 2010 @ 09:51
    Ian Smedley
    0

    Does your page have a <form runat="server"> tag on it?

    Looking at MSDN their example doesn't have a runat="server" in the head..

    http://msdn.microsoft.com/en-us/library/kx145dw2.aspx

     

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Sep 29, 2010 @ 09:51
    Matt Brailsford
    1

    Hey Warren,

    I presume you mean if you have multiple on the page. If so, just do a check before appending (either set a bool, or just check the dom), if it's already been added, don't do it again =)

    Matt

  • Warren Buckley 2106 posts 4836 karma points MVP ∞ admin hq c-trib
    Sep 29, 2010 @ 11:00
    Warren Buckley
    0

    @Lee are you saying we should just ask the user to manually add the CSS and JS to the head section themselves?

    @Matt OK that sounds like it might be the best idea for now

    Will get back to you all when I have the finished solution.

    Cheers,
    Warren

  • Casey Neehouse 1339 posts 483 karma points MVP 2x admin
    Sep 29, 2010 @ 14:04
    Casey Neehouse
    0

    To clarify what I was suggesting:

    When installing the package, process the templates to verify that head has runat="server".

    However, this would also require the templates to be checked with every save.  (Event?)

    A thought going forward: is it best practice to have runat="server" in the head, and if so, should umbraco automatically add it.  I know it causes .net to manipulate things such as titles.

  • Sjors Pals 617 posts 270 karma points
    Sep 30, 2010 @ 10:00
    Sjors Pals
    0

    why not puting instructions at the package  to manually place those scripts in the code?

     

  • Warren Buckley 2106 posts 4836 karma points MVP ∞ admin hq c-trib
    Sep 30, 2010 @ 10:04
    Warren Buckley
    0

    Hey Sjors I am tempted to go down this route, but it is nice to think about a dummy proof option as well.

    Warren :)

  • Sjors Pals 617 posts 270 karma points
    Sep 30, 2010 @ 10:26
    Sjors Pals
    0

    Well, for both something is to say. We use automatic builds, with minifying and combining css/js, and put the js at the bottom of the page...

Please Sign in or register to post replies

Write your reply to:

Draft