Inserting inline JavaScript in ASP.NET

Hi - I’m trying to insert a simple piece of Google Analytics tracking code into a .NET template which appears when a lightbox style shopping basket feature is triggered.

However, if I enter the following code into the template it simply does not appear - I just get whitespace:


<script type="text/javascript">
pageTracker._trackPageview("/addtobag/");
</script>

I’m a complete .NET novice, in fact I know less than that. I’d just like to understand why this doesn’t display.

Thanks! :slight_smile:

Can you post a more complete snippet? The important part is probably outside your code.

Thanks for replying, please see below:


<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Tenthtime.Models.Product>" %>
<div class="padding">

<script type="text/javascript">
pageTracker._trackPageview("/addtobag/");
</script>

    <div id='product_write_logo' style="float:none">
        <%= MerchantLogoHelper.GenerateLogo(Model.GetMerchant(), ViewContext) %>
    </div>
    <p>You have just added this product to your shopping bag.</p>
    <div id="with_border">
        <table cellpadding="10">
            <tr valign="middle">

                <td> <img src="<%= Model.ThumbUrl %>" /></td>
                <td>
                    <div class="pr_title">
                        <%= Html.Encode( Model.EncodedName) %>
                    </div>
                    <div class="pr_price">
                        £<%= Model.Price %>
                    </div>
                </td>
            </tr>
        </table>
    </div>
    <p>
        You can continue browsing our web site or go straight to <b class="pr_title"><%= Model.GetMerchant().Title %></b> to complete your purchase.
    </p>
</div>
<div id="continue_browsing">
    <table width="100%">
        <tr>
            <td align='left'  style="padding-left:5px;">
                 <a href="#" onclick="disablePopup(); return false;">Continue Browsing</a>
            </td>
            <td align="right" style="padding-right:5px;">
                <a target="_blank" href="<%= Url.Action("Buy", "Transition", new{ProductId = Model.Id}) %>" style="float:right" class="buy_now" rel="nofollow"></a>
            </td>
        </tr>
    </table>
</div>

Any ideas?

Wouldn’t this work? Or, maybe put it at the bottom of the page?



<%= Html.Decode(<javascript type="text/javascript">pageTracker._trackPageview("/addtobag/");</javascript) %>



Probably wrong though…

Not sure exactly what is going wrong here, but you are doing it wrong – you should wrap the trackPageView call in the client-scripty bits of whatever is adding to the cart, not try and spit it out. Javascript might be interperted, but it ain’t that interperted.

its too small and no functions are described
insert full code