开发者

javascript variable existence on page

I'm creating javascript Variable using c#.net inside code behind page and putting that variable on page using Page.ClientScript.RegisterClientScriptBlock(). So that variable is available on page and I can read(get) that variable value on client side using jquery.

I'm doing following:

C# :

category_columnNames += "var vertical_" + catIt开发者_StackOverflowem.VerticalID + "_columnNames=['Tools','PersonID','Topic','Category','Cost','Company']";    
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "LoadColumnNames", category_columnNames, true);

By doing this let's say :var vertical_1_columnNames=['Tools','PersonID','Topic','Category','Cost','Company']; is on page. Now I want to first check if that variable(vertical_1_columnNames) exists on page or not. If yes then I need to get value of it (['Tools','PersonID','Topic','Category','Cost','Company']) on client side.I'm doing following on client side:

Client Side :

function ViewCartDirectLeadsGridInit(gridID) {

     alert(gridID);//vertical_1_CategoryGrid

     var vertical = gridID.toString().split('_')[1];
     var columnNames = "vertical_" + vertical + "_columnNames";

     alert(columnNames); // vertical_1_columnNames
     alert(typeof(columnNames));// string 
     alert(eval(columnNames)); // ['Tools','PersonID','Topic','Category','Cost','Company']

      if (!window.columnNames) // This is not working.I want to check for existence of var vertical_1_columnNames
      {
       alert("success");
       return false;
       }
      else{
      // do something;
      }

 }          

Any suggestion?

Thanks,

A


window.columnNames will search for a variable named "columnNames" not "vertical_" + vertical + "_columnNames". Use window[columnNames].

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜