Page 1 of 1

Javascript access to Product Variables in an HTML scriptlet

Posted: Wed Oct 07, 2009 4:50 pm
by bl108
Hello,

Is there a way to get Javascript access to product variables in an html scriptlet ?

For example:

<h2>$Product.Name</h2>
...
<script type="text/javascript">
document.write($Product.Name); <- Doesn't work ?
</script>


Thank you.

Re: Javascript access to Product Variables in an HTML scriptlet

Posted: Thu Oct 08, 2009 2:44 am
by mazhar
Following script is working for me on product details page.

Code: Select all

<script language='javascript' type='text/javascript'>
alert('$Product.Name');
</script>
Keep in mind one thing that NVelocity variables are available in contexts for example $Product represents current product and is available on product details page. If you try to run same script on category page it wont return any thing because no product is available in that context.

Re: Javascript access to Product Variables in an HTML scriptlet

Posted: Thu Oct 08, 2009 5:44 am
by jmestep
This is a similar question-- How do I passthe productId in the querystring below?
<asp:ImageButton ID="PrintPage" CausesValidation="false" runat="server" Text="Print Page" EnableViewState="false" ImageUrl="~/images/buttons/print-page.gif" OnClientClick="window.open('printproduct.aspx?ProductId=xx','mywindow','width=800,height=600,scrollbars=yes')" />
This is in a conlib and it's picking up the productid OK from the code behind because I can display the produtid in another control using PID.Text = _ProductId.ToString()

Re: Javascript access to Product Variables in an HTML scriptlet

Posted: Thu Oct 08, 2009 6:05 am
by mazhar
set OnClientClick in back end code file like

Code: Select all

PrintPage.OnClientClick= string.Format("window.open('printproduct.aspx?ProductId={0}','mywindow','width=800,height=600,scrollbars=yes')",_Product.ProductId);

Re: Javascript access to Product Variables in an HTML scriptlet

Posted: Thu Oct 08, 2009 8:43 am
by jmestep
Thanks, that worked.

Re: Javascript access to Product Variables in an HTML scriptlet

Posted: Thu Oct 08, 2009 12:06 pm
by bl108
Mazhar,

The single quotes around the variable reference works.

Thank you very much !!!

Re: Javascript access to Product Variables in an HTML scriptlet

Posted: Wed Jun 16, 2010 1:30 pm
by MMIKAL
mazhar wrote:Following script is working for me on product details page.

Code: Select all

<script language='javascript' type='text/javascript'>
alert('$Product.Name');
</script>
Keep in mind one thing that NVelocity variables are available in contexts for example $Product represents current product and is available on product details page. If you try to run same script on category page it wont return any thing because no product is available in that context.

Actually the '$Product.Name' in the JavaScript does not work, the same variable works find outside the js in the same page.
Any idea why?