0

I have a string structure like this:

    string str = "[['<h1>Heading 1</h1><p>Paragraph 1</p><p>Paragraph 2</p><p>Paragraph 3</p>', 31, 32,1],
['<h1>Heading 1</h1><p>Paragraph 1</p><p>Paragraph 2</p><p>Paragraph 3</p>', 34, 35,2]]";

I am trying to pass this string in javascript function but alert is not working. How can I pass this string in JS function. So far I have tried this but it is not working

ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "test", "test('" + str + "')", true);

JS

 function test(str) {

    alert(str);
 }
2
  • 1
    Check the generated javascript, it contains embedded quotes that end the string prematurely. You will need to escape (at least) the quotes. Commented May 11, 2015 at 14:22
  • @HansKesting string has proper quotes I have already checked this. Commented May 11, 2015 at 14:26

2 Answers 2

1

When you check the generated javascript, it will start with test('[[' - and then the rest is not recognised as string content and gives a javascript error.

You will need to escape the string, using HttpUtility.JavaScriptStringEncode:

ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "test",
 "test('" + HttpUtility.JavaScriptStringEncode(str) + "')", true);
Sign up to request clarification or add additional context in comments.

Comments

0

you can use native browser JSON stringify function.. like this..

function test(str){
    alert( JSON.stringify( str ) ) ;
}

you should get your array in popup..

hth

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.