You must Sign In to post a response.
  • Category: JavaScript

    Get global variable in external js page

    hi all

    i have created a class.cs page and passing a value from first.aspx.cs page to class.cs.

    And i am getting the global variable in second.aspx.cs page.

    But i want to get that global variable in external js page.

    Please tell me the code for that.

    for getting the global variable in second.aspx.cs page by using the below code

    string globalvalues = Globalvar.GlobalValue;-------------------------this is working fine

    in js page i have used the below 3 types of codes


    var globalvalues = $('#Globalvar.GlobalValue');

    var globalvalues = '<%= Globalvar.GlobalValue %>';

    var globalvalues = '@Globalvar.GlobalValue';

    ----------------------------these are not working
  • #764080
    Where you are referring the js file?. In the header tag?

    Can you try to refer that js fine the bottom of the page body?

    By Nathan
    Direction is important than speed

  • #764148
    Hi rishe,

    1) Define your global variable in code behind BEFORE calling external js function in which you are going to access it:
    Page.ClientScript.RegisterStartupScript(this.GetType(), "SetValue", string.Format("var GlobalVar = '{0}';", "DemoText"), true);
    2) Call and access global variable inside external js as:
    if (typeof (GlobalVar) !== "undefined")

    Hope this will help you.
    Shashikant Gurav

  • Sign In to post your comments