I tried reproducing your problem, but for me things seem to work fine. So I'll describe what I have in hopes that it helps you solve your problem:
Schema
I have a Schema with these fields:
- LinkedComponent - a Component Link Field
- ImageToDisplay - a Multimedia Component Link Field
- OnClick - a single-line text field
Component
I created a Component based on this schema that links to some other Component and Image in my Tridion instance and has your script in the OnClick field.
This is the XML for that Component:
<Content xmlns="uuid:9e69dd45-f249-42a5-9f9a-5f68478b106f">
<LinkedComponent xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple"
xlink:href="tcm:1-91" xlink:title="Xbox360Kinect"></LinkedComponent>
<ImageToDisplay xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple"
xlink:href="tcm:1-90" xlink:title="Kinect"></ImageToDisplay>
<OnClick>var s=s_gi(s_account);s.linkTrackVars='eVar7';
s.eVar7='optin.holidays';s.tl(this,'o',Email Signup | Holidays')</OnClick>
</Content>
Note that I just added a line-break in the OnClick
value for readability. This line-break is not present in the original XML.
DWT
Next I creates this DWT to output the link:
<a title="Holidays" tridion:href="@@LinkedComponent@@" onclick="@@OnClick@@">
<img title="btn_email_sign_up" alt="btn_email_sign_up" src="@@ImageToDisplay@@"/>
</a>
Once again, any line-breaks in the above fragment were added for readability only. The DWT in my Tridion system has all this on a single line.
Component Template, Page Template and Publication Target
I use this DWT in a Component Template that is set to output an HTML fragment. Aside from the DWT, this Component Template only contains Default Finish Actions (after the DWT of course).
The Page Template I used is a copy of the regular Default Page Template, where I just changed the file name extension to aspx
.
The Target Language of my Publication target is set to ASP.NET.
Published ASPX
All of these steps lead to the following fragment in my published ASPX page:
<tridion:ComponentLink runat="server" PageURI="tcm:1-514-64" ComponentURI="tcm:1-91"
TemplateURI="tcm:0-0-0" AddAnchor="false" LinkText="<img
src="/GameShop/Images/Kinect.jpg" title="btn_email_sign_up"
alt="btn_email_sign_up"/>" LinkAttributes=" title="Holidays"
onclick="var s=s_gi(s_account);s.linkTrackVars='eVar7';s.eVar7='optin.holidays';s.tl(this,'o',Email Signup | Holidays')""
TextOnFail="true"/>
Here too I added some line-breaks to slightly improve readability. In the original ASPX page, this entire block is on a single line.
HTML in Browser
Finally this leads to the following HTML in the browser:
<a href="/GameShop/Xbox360/Accessories/Xbox360Kinect.aspx" title="Holidays"
onclick="var s=s_gi(s_account);s.linkTrackVars='eVar7';s.eVar7='optin.holidays';s.tl(this,'o',Email Signup | Holidays')">
<img src="/GameShop/Images/Kinect.jpg" title="btn_email_sign_up"
alt="btn_email_sign_up">
</a>
I am not sure if the onclick
script gets triggered, but I don't think that was your initial question. Despite a ton of escaping going on at various layers, the script ends up in the HTML fine - at least to my eyes.
Don't mix JavaScript and Content
That leaves one big question though: why on earth are you trying to put JavaScript in a field of the Content? That violates all best practices of separating Content from Code and just means you're setting yourself up for maintenance problems in the future (e.g. what if you want to start sending out emails?).
Having clicking-on-links tracked by analytics software is a valid requirement of course (in fact, it has been a feature of Tridion's Content Delivery stack since as long as I can remember).
But I suggest implementing it in a different way, for example by simply marking each such link with a specific CSS class or an HTML5 data attribute:
<a href='...' data-tracking-label='Signup | Holidays'
data-tracking-key='optin.holidays' ...
Then you can make the JavaScript to find these links generic and just put it in the regular JavaScript files that your web site undoubtedly already has.
See also this question from Manoj and Quirijns answer: How to Implement Google Analytics in SDL tridion web sites?