Copied to clipboard

Flag this post as spam?

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


  • Thomas Kahn 602 posts 506 karma points
    Nov 16, 2010 @ 10:45
    Thomas Kahn
    0

    Generate dynamic javascript and add it to a .js file?

    Hi!

    I'm working on a site where I'll make quite heavy use of javascript and at the same time I will need to fetch data from Umbraco. Yes, I could create an XSLT-script that generates javascript code and put it as a macro in the template. But if the macro just prints the javascript, it would be added to the page as a page script like this:

    <script type="text/javascript">
    //My script code goes here
    </script>

    ...and browsers don't cache these scripts like they do when you have them in a separate file and included on the page like this:

    <script type="text/javascript" src="myScript.js"></script>

    The second idea I have is to use a regular .js file and have it load data using AJAX from a resource page that I set up in Umbraco. This page would just contain an XSLT-script that generates JSON, HTML-snippets or whatever I need. Still it would mean an extra call to the server.

    Is there a smarter way that I haven't thought of?

    Regards,
    Thomas Kahn

  • Steen Tøttrup 191 posts 291 karma points c-trib
    Nov 16, 2010 @ 10:51
    Steen Tøttrup
    0

    One way of doing this, involves making a HttpHandler. I've used in it a number of projects, and you can take a look at it here: http://dic2js.codeplex.com/SourceControl/changeset/view/54174#1192594

    It might not be the prettiest piece of code, but it should show you what I'm talking about.

    This is part of a package I've done, that generates javascript using xml (data from Umbraco) and xslt.

    regards,

    Steen

  • Sean Mooney 131 posts 158 karma points c-trib
    Nov 16, 2010 @ 14:20
    Sean Mooney
    0

    @Thomas, 

    You could do just as you are saying, create a template that has a macro on it to out put js then add it to the page in the following manner:

    <script type="text/javascript" src="MyTemplateName.aspx"></script>

    You might also thing about setting the content type, by putting this at the top of your macro.

    <xsl:value-of select="umbraco.library:ChangeContentType('text/javascript')" />

    I have found this to be the easiest way to add javascript, it is also great for create dynamic XML to feed to a flash file

    -Sean

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies