0

I have a windows Dotnet application with a webbrowser Control. on a button click it calls to a javascript function with parameter and javascript try to convert that received parameter to array. Below is my C# and JavaScript Code

public Form1()
        {
            InitializeComponent();

            webBrowser1.DocumentText = @"<html><head>
                <script type='text/javascript'>
                    function doIt(val) 
                    {
                        data = String(val);
                        chartval = new Array(data);
                        //alert('Method Invoked..................' + data);
                        for (var i = 0; i < chartval.length; i++) 
                            {
                               alert('invoked.....'+chartval[i]);
                            }
                        return 'i did it!';
                    }
                </script>
                </head><body>hello!</body></html>";

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string tempstring="[abcd],[efgh],[ijkl]";
            string []objarr=new string[1];
            objarr[0] = tempstring;

            object y = webBrowser1.Document.InvokeScript("doIt", objarr);
            MessageBox.Show(y.ToString());
        }

here tempstring has thousand values. I want to convert the tempstring value to a javascript array

now I have alert having text
invoked..... [abcd],[efgh],[ijkl]
I want 3 alert with text

invoked..... abcd
invoked..... efgh
invoked..... ijkl
3
  • what are these [...] signs for? I don't see any reason here to use them but one, as if you have a 2 dimensional array like [[...],[...],[...]], I think you have just a simple array like ["abcd","efgh","ijkl"], please correct me if I'm wrong! Commented Jan 25, 2014 at 10:16
  • @MehranHatami this is just a model snippet based on my project. I gets this kind of array 'string tempstring="[abcd],[efgh],[ijkl]"' from the system, so I had use it as it is and in actual it contains thousands of values like that. Commented Jan 25, 2014 at 10:45
  • Possibly a duplicate: Loading and Passing JScript Arrays from/to C# (not C) COM Component. Commented Jan 26, 2014 at 5:21

1 Answer 1

0

in your javascript do this:

function doIt(val){
    val = String(val);
    var chartval = val.substring(1, val.length - 1).split("],[");
    for (var i = 0; i < chartval.length; i++){
        alert('invoked.....'+chartval[i]);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

this is just a model snippet based on my project. I gets this kind of array 'string tempstring="[abcd],[efgh],[ijkl]"' from the system, so I had use it as it is and in actual it contains thousands of values like that.

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.