1

Im having a variable 'multi' , the same name i used within the js file. I need to set a value to the variable within my aspx page and pass it on to the javascript file. Couldnt find any help.

aspx script code

    var itemdata = [];
    var multi;
    //var plot;
    $(document).ready(function () {
        $.ajax({
            type: "POST",
            url: "ChartBinder.asmx/BindChart",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                //alert(multi);

                var resultObj = $.parseJSON(response.d);
                multi = resultObj;
               // alert(resultObj.Pie);
                multi.Trend = resultObj.Trend;
                //alert(multi.Trend);
                $.getScript('Scripts/highcharts.src.js', function () {
                    DrawPie(resultObj.Pie);
                    DrawTrend(resultObj.Trend);
                    // do something here

                });

            },
            error: function (msg) {
                alert("Error");
            }
        });
    });

js script

(function () {
   var seriesCount = window.multi.Trend.length;

   var newcolors = [];

   for (i = 0; i < seriesCount; i++) {
      newcolors[i] = multi.Trend[i].color;
   }
})
5
  • 1
    If you declare var multi (global scope) before you include the .js file then you should already have access to it from the .js include.
    – webnoob
    Jan 10, 2013 at 13:20
  • Im loading the script only after fetching the data. Still not getting. Can u check the updated code?
    – NewBie
    Jan 10, 2013 at 13:31
  • Can you show your entire html code that applies (more specifically, the areas where the inline JS is written and where the include is brought in)
    – webnoob
    Jan 10, 2013 at 13:33
  • 1
    Off topic: You don't need to do the multi.Trend = resultObj.Trend; assignment as you have already done multi = resultObj;. On Topic: Does alert(multi.Trend); produce the correct value? Also, in your .js file you are using window.multi.Trend.length; Can you try without window. (although I am not exactly sure how the global var will work with a file brought in via AJAx.
    – webnoob
    Jan 10, 2013 at 13:43
  • @webnoob... Tried like what you mentioned. It worked. Thanks a lot :)
    – NewBie
    Jan 11, 2013 at 3:49

3 Answers 3

0

multi should be in scope globally for use within that your include because you have declared it in Global scope i.e the top of the document outside of a function block.

Try changing

var seriesCount = window.multi.Trend.length;

to

var seriesCount = multi.Trend.length; 

Off Topic:

You don't need to do the multi.Trend = resultObj.Trend; assignment as you have already done multi = resultObj;.

0

multi in your example is declared in Global scope therefore available to any other JS in your applicaiton:

var multi; //global but must be included before code referencing it

(function () {
    var multi; //local
})

Based on your comments:

....
$.getScript('Scripts/highcharts.src.js', function () {
      DrawPie(resultObj.Pie);
      DrawTrend(resultObj.Trend);
      // do something here
      DoWork();
 });
 ...

 function DoWork()
 {
     var seriesCount = window.multi.Trend.length;
     var newcolors = [];
     for (i = 0; i < seriesCount; i++) {
       newcolors[i] = multi.Trend[i].color;
     }
 }
2
  • Can you confirm that is still the case when the file has been loaded via JQuery $.getScript('Scripts/highcharts.src.js', function () {})
    – webnoob
    Jan 10, 2013 at 14:09
  • rick schott... It's not working. What you'd written under the DoWork part is actually within the js file which im loading dynamically :(
    – NewBie
    Jan 11, 2013 at 3:45
0

Declare a variable of required datatype in your code behind(.aspx page)

In Code behind(.aspx page):

private string multi = "New";

public string Multi
{
    get { return multi; }
}

In javascripts:

var multi='<%=Multi%>';
//Do your stuffs here
1
  • This doesn't have anything to do with the code behind. It's a scope issue with JS vars. The ASPX part is misleading, he just means a HTML page.
    – webnoob
    Jan 11, 2013 at 7:07

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.