Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sharing a variable between JS and XSL

Status
Not open for further replies.

misterpunch

Technical User
Nov 18, 2007
6
CA
Through a little more trial and error I've gotten two parts of my site working, now I just need both sides to talk to each other and my task is complete.

I apologize for I am mostly self taught and I'm probably missing fundamental rules of syntax, but I thought I was pretty close to a solution but I've hit a brick wall. Any guidance would be appreciated.

My goal: To sort a list of pictures by filtering out one category (category element in the XML) the Sorting would happen when a function changed the value of the JavaScript Variable. What I need to do is have the XSL recognize the variable and apply the correct template to filter the list accordingly. Both HTML and XSL are linked to the same external .js file which contains (so far):
Code:
var active;

var gallery=new Array();
gallery[0]="nofilter"
gallery[1]="portrait"
gallery[2]="product"

if (active = "null")
{
var active= gallery[0];
}

function test()
{
alert("Variable is currently: " +active );
}

function setValue(id)
{
active = id;
}


when gallery[1] is becomes active I would want this applied:
Code:
<xsl:for-each select="gallery/photo[category='portrait'">
<xsl:sort select="idn" order="descending" />

here is the site again.
 
[1] >if (active = "null")
Two problems.
[1.1] If you want to compare active with "null" you should use:
[tt]if (active [red]==[/red] "null")[/tt]
[1.2] Even if active is not initiated to anything, this comparison is not what you would expect resulting in true. Instead you can do this as a proxy.
[tt]if (!!active)[/tt]

[2] In the first part you assign in the if being evaluated to true:
active = gallary[1]
In the following function, you assign active to id
active = id
If id being an integer, you are effectively assigning different thing to active. (But if id being some gallery entry, then no problem. If id is a number, you might mean to set active to gallery[id]. Just can't be sure.)

[3] As to another lead seemingly what you might want to proceed, you can use addParameter method to xslt process. Hence the kb article.
In your previous thread
which is as clear as mud, you have a top level parameter
[tt]<xsl:param name="navigation" />[/tt]
It is in fact stand ready to receive its value through js via the method. But there, in the stylesheet, I find nowhere referencing to it other than the xsl:param element! You may therefore look into that direction.
 
Thanks for the quick and helpful reply. I've since brushed up on my JavaScript and can pick up where I left off with the Param. It seemed like the easiest thing to do, so maybe I was on the right track with my shoddy code!

I apologize that I'm an animator and not a programmer/scripter but I'm learning.
 
[2] In the first part you assign in the if being evaluated to true:
active = gallary[1]
In the following function, you assign active to id
active = id
If id being an integer, you are effectively assigning different thing to active. (But if id being some gallery entry, then no problem. If id is a number, you might mean to set active to gallery[id]. Just can't be sure.)


"id" would be the gallery entry, yes. I've renamed the array "option" for clarity. My plan was to populate JS variable "Active" with my selected choice from the array "option". Then, in the XSL I could have the Param equate to the JS variable and use that to filter the <xsl:for-each>
Code:
 <xsl:for-each select="gallery/photo[category=$param]">

My question is that if it were that simple, would my xml file be updated just as the value of the JS variable changes? My guess would be no and that I'd have to actively have it re-transformed in the frame. (which is a whole new set of problems for me).

Another problem I've had was returning the default value of the param.

Code:
<xsl:value-of select="$param" />
this outputs nothing in the frame at all.

Code:
<xsl:param name="param" select="product" />
is above the template to make it a global param with a default value that should filter my <xsl:for-each> as though i hard-coded it. Instead it returns nothing and the <xsl:for-each> is omitted all together.

 
Perhaps you might try:

Code:
<xsl:param name="param" select="[COLOR=white blue]'[/color]product[COLOR=white blue]'[/color]" />

Tom Morrison
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top