user1740381
user1740381

Reputation: 2199

Issue with multiple quotes in string

I have the following string in c# code.

 string home = "<img src='/images/Home.png' 
                     onclick='javascript:document.location.href=/Home/Index' />";

When i send this string to client side then click event on image is not working, giving the following error in the console :

Uncaught SyntaxError: Invalid flags supplied to RegExp constructor 'Index' 
page_home (1):1 onclick

I have a feeling that there is something wrong with quotes in the string but i didn't get. What i am doing wrong here ?

Upvotes: 0

Views: 724

Answers (1)

Jamie
Jamie

Reputation: 4980

The issue is you haven't encapsulated the location in quotes in the javascript so it's processing it as a regular expression.

This should solve your issue.

string home = "<img src=\"/images/Home.png\" onclick=\"javascript:document.location.href='/Home/Index'\" />";

You can also do this:

string home = @"<img src=""/images/Home.png"" onclick=""javascript:document.location.href='/Home/Index'"" />";

Upvotes: 3

Related Questions