1

Basically I have a string variable and i pass this string variable to javascript function using the code below.

Chart1.Series["Series1"].Points[counter].MapAreaAttributes = "onmouseover=\"showAlert("+tempString+",event);\"";

my javascript function is as follows:

function showAlert(stringVal,ex) {
       //var temp = document.getElementById("HTxtFieldPopIp").value;
      // temp = "testing";
      // alert(temp);
        alert(stringVal);
   }

But this doesnt give me a alert box.

When i remove the parameter and run the commented pieces of code the same happens. Any suggestions.

3
  • Does this function gets fired? If so,try adding return false inside function showAlert; Commented May 29, 2013 at 4:52
  • stackoverflow.com/questions/2223543/… Commented May 29, 2013 at 4:53
  • Are you using ASP.Net or MVC as front end? Commented May 29, 2013 at 5:22

2 Answers 2

3

You need to quote the string:

"onmouseover=\"showAlert("+tempString+",event);\""

becomes:

"onmouseover=\"showAlert('"+tempString+"',event);\""

So, if tempString equals foo then the js dynamically generated would be:

onmouseover="showAlert('foo',event);"
Sign up to request clarification or add additional context in comments.

3 Comments

+0. While correct, using properly encoded string would be much safer - current sample is likely cause XSS (old MSDN article on XSS and method that should be used now to construct the inner portion of string JavaScriptStringEncode.
He's just doing a silly alert on a tempString. I was answering why it wasn't working :)
+1. Probably you are right, I should not expect people to care about it :).
2

I think you need quotes around tempString. Your C# code now results in:

onmouseover="showAlert(testing,event)"

Change it to

Chart1.Series["Series1"].Points[counter].MapAreaAttributes = "onmouseover=\"showAlert('"+tempString+"',event);\"";

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.