Is there an easy way to add JavaScript to a child template? I would put it inside the SCRIPT tag in the HEAD tab but that's in the main parent template.
I don't want the JavaScript in the main template, because it needs to be different for each of my several child templates.
How about adding another ContentPlaceHolder within the html head section of your parent template, that way on the child templates you can 'inject' your javascript into that part of the markup.
Maybe you could consider making a header.xslt-macro. This macro could be used to create some special (or all) of the information in the -tag like meta-tags, references to stylesheets, scripts etc.
Then in this XSLT file you could make a check to see if a page uses the template(s) where you want to include the script.
Otherwise I'm pretty sure that you can create a asp placeholder in your <head>-tag, and then insert the script into this palceholder, from your child template.
Yes, that's another option, and could be achieved in the same manner - using ContentPlaceHolders - eitther by setting a global js value, or better yet calling the main JS routine from the child template, and passing in a parameter.
Can I ask what value you want to set on the child template ? Perhaps the approach suggested by Kim would be more approprite as the common JS could be within the macro, and a check placed such that it's only rendered if it's required ?
I created the jQuery script below, which shows or hides navigation menus based on what type of page it is.
I want to set the PageType variable in the child template, so it knows which menus to show for that type of page.
As I think about it, the easiest solution seems to put this Javascript in the main template, and then pass the PageType variable somehow from the child template. I'm trying to figure out how to do that now. But I am also open to your other suggestions here.
The following would work, but your code leads to a few more questions, do you have another scripts to show / hide the nav areas if the user interacts with them ? if not, might be better to only render out those navs that are in use and not use any javascript - if they are used elsewhere, how about hiding them all by default, and then show the relevant ones ? a toggle method could shrink the code quite significantly.
Add JavaScript to child template?
Is there an easy way to add JavaScript to a child template? I would put it inside the SCRIPT tag in the HEAD tab but that's in the main parent template.
I don't want the JavaScript in the main template, because it needs to be different for each of my several child templates.
Hi Tesuji,
How about adding another ContentPlaceHolder within the html head section of your parent template, that way on the child templates you can 'inject' your javascript into that part of the markup.
HTH,
Hendy
Hi,
Typically I handle this by adding a ContentPlaceHolder in the HEAD tag called something like "AdditonalHeadHTML"
Then you can inject javascript/CSS related to a child template by using that placeholder.
Ex, in your master template:
In a child template:
-Tom
Hi Tesuji
Maybe you could consider making a header.xslt-macro. This macro could be used to create some special (or all) of the information in the -tag like meta-tags, references to stylesheets, scripts etc.
Then in this XSLT file you could make a check to see if a page uses the template(s) where you want to include the script.
Otherwise I'm pretty sure that you can create a asp placeholder in your <head>-tag, and then insert the script into this palceholder, from your child template.
/Kim A
Another option I have is to put the main Javascript in the main template and then just set a Javascript variable in the child page.
Is this hard to do - pass a Javascript variable from the child template to the to the main template?
Hi Tesuji,
Yes, that's another option, and could be achieved in the same manner - using ContentPlaceHolders - eitther by setting a global js value, or better yet calling the main JS routine from the child template, and passing in a parameter.
Can I ask what value you want to set on the child template ? Perhaps the approach suggested by Kim would be more approprite as the common JS could be within the macro, and a check placed such that it's only rendered if it's required ?
Cheers,
Hendy
I created the jQuery script below, which shows or hides navigation menus based on what type of page it is.
I want to set the PageType variable in the child template, so it knows which menus to show for that type of page.
As I think about it, the easiest solution seems to put this Javascript in the main template, and then pass the PageType variable somehow from the child template. I'm trying to figure out how to do that now. But I am also open to your other suggestions here.
Sorry the first page of my jQuery should be the following. I want to pass the variable thisPageType
Can't you just call the javascript function from your template/macro, with the ritght value. Something like this:
Where the thisPageType is the correct type of course. Shouldn't this work or am I missing something obvious here?
/Kim A
Hi Tesuji,
The following would work, but your code leads to a few more questions, do you have another scripts to show / hide the nav areas if the user interacts with them ? if not, might be better to only render out those navs that are in use and not use any javascript - if they are used elsewhere, how about hiding them all by default, and then show the relevant ones ? a toggle method could shrink the code quite significantly.
Anyway, hope the following helps :)
Parent template:
Child template:
Cheers,
Hendy
Hendy, in your latest post, I assume I'll need to put that jQuery code for the child template in SCRIPT tags?
Hi Tesuji,
Oops, yes, (and also include the jQuery library in the parent template).
(Alternatively you could move the ContentPlaceHolder up into the <script> tags and rename it to something like javascriptContentPlaceHolder)
Cheers,
Hendy
OK, I'll try that.
Thanks, everyone for your replies. I'm a Javascript newbie and I'm still digesting everything that's been said here.
is working on a reply...