0

I have a HyperLink on my usercontrol in which I set the onlick event dynamically on the server side like this :

this.Attributes["onclick"] = string.Format("javascript:alert('{0}')", base.NavigateUrl);

The problem is that when Asp.net renders the page, it ends up with something like this

<a href='...' onclick="javascript:alert(&#39;TEST&#39;)>LINK</a>

which obviously is not valid Javascript. Using " instead of ' wouldn't help neither, the generated HTML is alert(&quot;TEST&quot;)

Is there any way around this?

Thank you.

3 Answers 3

3

onclick="javascript:alert(&#39;TEST&#39;)" is perfectly valid. When the page is parsed, entities should be replaced with their counterparts before JavaScript processes the value. So in pass 1, the HTML parser will change the attribute value to javascript:alert('TEST');, which is valid JavaScript.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh thanx, I've been inspecting the HTML directly
1

It is not working when using Jquery.

Server side:

this.Attributes["onclick"] = "$('#button').click();";

Client side:

onclick="$(&amp;#39;#button&amp;#39;).click();" gives an error.

Is there anyway to just get 'real' single quotes client side?

Comments

0

royu,

Unfortunately, Microsoft thinks this is a "security feature" and they don't really care about browser or JavaScript compatibility. One way to override it is to use Eval([string]) instead of the plain text. In that case the value should not be encoded.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.