Home
TeamSite
including Javascripts in TPL's
dips
Hi,
I want to include Javascript files in my TPL and then call a function in the .js file. To do this I am including the following lines in my TPL:
<script language="text/javascript" src="/assets/scripts/nextel_functions.js"/>
<script language="text/javascript" src="/assets/scripts/nx_localnavdef_en.js"/>
<div id="localNavBox" class="mod1Wid">
<script language="javascript" location="template-type">
<![CDATA[
renderLocalNav("31");
alert("in script");
]]>
</script>
</div>
Problem is that I don't think that the script is being included as I have added some alert messages in the .JS files which do not show up when the TPL is previewed.
Thanks in advance,
Dipika
Find more posts tagged with
Comments
Adam Stoller
Are you trying to run javascript within the PT? Or do you want to have javascript code embedded in the *output* from the PT - which is generally an HTML page which gets viewed via a browser - which can in turn execute the javascript code ?
The former doesn't make sense to me - so if you're trying to do that you need to supply a LOT more inforamation about what you're trying to do and why.
The latter makes sense and is relatively trivial - if you wanted to do this but were trying to do it in the manner of the former, try again, and then post if it still doesn't work.
--fish
Senior Consultant, Quotient Inc.
http://www.quotient-inc.com
dips
Hi,
Thanks for the quick response. I am trying the former condition. There is a variable 'localNavId' which i'll be getting from the dcr. Using this id I would be rendering the html for a local navigation section using renderLocalNav() in nextel_functions.js. This function uses the localNavDef[] array defined in nx_localnavdef_en.js
In the TPL-
<script language="text/javascript" src="/assets/scripts/nextel_functions.js"/>
<script language="text/javascript" src="/assets/scripts/nx_localnavdef_en.js"/>
<div id="localNavBox" class="mod1Wid">
<script language="javascript" location="template-type">
<![CDATA[
renderLocalNav(dcr.localNavId);
]]>
</script>
</div>
The function renderLocalNav() in nextel_functions.js that I am calling in my TPL-
function renderLocalNav(localNavID){
if(localNavDef[localNavID] == null){
document.writeln("There was an error in processing your request. Navigation ID not found.");
return;
}
var navID = localNavID + "";
var idArray = new Array();
var navLevels = navID;
var baseNum = parseInt(localNavDef[navID][0]);
var baseID = "";
var clsID = "";
var secNum = "";
if(localNavDef[navID][0].indexOf('.') > -1){
navLevels = localNavDef[navID][0].split(".");
secNum = navLevels[0]+"."+navLevels[1];
}
var tier2Num = 0;
var tier3Num = 0;
for(x in localNavDef){
if(localNavDef[0].indexOf(baseNum+".") == 0){
idArray.push(x);
}
if(localNavDef[0] == baseNum+""){
baseID +=x;
}
}
idArray.sort(sortLocalNavList);
document.writeln('<div class="moduleSpacer mod1Wid">\n');
document.writeln('<div style="margin:0 10px 5px 10px"><a href="'+localNavDef[baseID][2]+'" class="blackBCText">'+localNavDef[baseID][1]+'</a></div>\n');
document.writeln('<div class="hDotDivLight"><img src="assets/images/spacer.gif"></div>\n');
for(x=0; x<idArray.length
{
navLevels = localNavDef[idArray][0].split(".");
var secID = idArray;
document.writeln('<div class="m10lr5tb">\n');
clsID = (navID == secID) ? "blackBText" : "greyBText";
document.writeln('<div class="'+clsID+' m5b">> <a href="'+localNavDef[secID][2]+'" class="'+clsID+'">'+localNavDef[secID][1]+'</a></div>\n');
var cnt = x+1;
while((idArray[cnt]!=null) && (localNavDef[idArray[cnt]][0].split(".")[1] == localNavDef[idArray][0].split(".")[1])){
var t2ID = localNavDef[idArray[cnt]][0].split(".")[0]+"."+localNavDef[idArray[cnt]][0].split(".")[1];
if(t2ID == secNum){
var thID = idArray[cnt];
clsID = (navID == thID) ? "blackText":"greyText";
document.writeln('<div class="m5b" style="margin-left:15px;"><a href="'+localNavDef[thID][2]+'" class="'+clsID+'">'+localNavDef[thID][1]+'</a></div>\n');
}
cnt++;
}
x = cnt;
document.writeln('</div>\n');
document.writeln('<div class="hDotDivLight"><img src="assets/images/spacer.gif"></div>\n');
}
document.writeln('</div>\n');
localNavDef = new String();
}
Thanks
Dipika
Adam Stoller
I think you need to clear your mind, take a step or two back from your computer, and think about this a bit more.
To quote (or at least paraphrase) Jon **** - "The PT is an arbitrary program that takes arbitrary data [the DCR] and produces arbitrary output [the generated file]"
The PT is not processed by a browser - and as such, javascript in a PT does not mean anything. If you want to have the PT do different things based upon information contained in the DCR its processing - you do that either via PT-XML directives or [frequently] Perl code embedded within the PT - and this will result in differnt content being output into the generated files.
The generated file is [usually] rendered by a browser, and as such javascript *does* make sense there. In this case, you would include the javascript code in your PT - but *not* in the sense of it being interpreted in the PT - only in the case of the code being output into the generated file where it will be interpreted *later* when the generated file is viewed via a browser. In this situation you could generate different javascript code based on the values contained in the DCR.
Does that make sense?
Perhaps the latter is what you're doing (I didn't bother looking at your code - I'm responding to the fact that you indicated you are trying to get javascript to function *within* the PT -- which I don't believe works)
--fish
Senior Consultant, Quotient Inc.
http://www.quotient-inc.com
Migrateduser
Since Javascript only relates to the client/UI experience, why and how would you want to include this in the TPL functionality, which is purely server-side? Any variable that you would want to use in an output file should already be known by the presentation template, which you can very easily pass to the output file by using, among other things, the <iw_value ...> tag.
I believe what you want to do is the following:
<script language="Javascript">
renderLocalNav('<iw_value name='dcr.localNavId'/>');
</script>
Dave
Current Environment(s):
(1) TS 6.1 SP1 on W2K3
(2) TS 6.1 SP1 on W2K
(3) TS 5.5.2 SP2 on Win2K