locked
Javascript File with Code <%= variable%> RRS feed

  • Question

  • User1557998713 posted

    I have several .aspx pages that use the same javascript functions. These functions include code <%= variable%>. I want to pass these functions to a .js file but they are not displayed correctly with Visual Studio and also the functions are not executed.

    // --------------
    // .Aspx
    // --------------
    <head runat="server">    
       <script type="text/javascript" src="../js/Js_Common.js"></script>
    </head>
    
    <script language="javascript" type="text/javascript"> 
      var myvariable = fcn_variable();
    </script>
    
    // --------------
    // Js_Common.js
    // --------------
    function fcn_variable()
    {   var myvariable = null;
        myvariable = '<%=Form_Label.ClientID%>';
        return myvariable;
    }
    
    

    Wednesday, February 19, 2020 6:11 AM

Answers

  • User475983607 posted

    JavaScript files are text files, not part of the ASPX page, and not processed by the framework. 

    Use standard programming techniques .  The simplest is passing the id to the function.

    function fcn_variable(id)
    {   var myvariable = id;
        return myvariable;
    }

    In the aspx page.

    <script language="javascript" type="text/javascript"> 
      var myvariable = fcn_variable('<%=Form_Label.ClientID%>');
    </script>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 19, 2020 12:16 PM