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:
<scripttype="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:
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.
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:
...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:
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
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
@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:
You might also thing about setting the content type, by putting this at the top of your macro.
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
is working on a reply...