Reputation: 604
I have a JavaScript function that I wrote and embedded in an .aspx page. I declared variables at the top of the function that access ConfigurationManager.AppSettings and also the sessionState tag as follows:
var Value1 = "<%= System.Configuration.ConfigurationManager.AppSettings["Value1"].ToString()%>";
var Value2 = "<%= Session.Timeout %>";
This function has been working just fine. I've realized that I will need to use the function across four other pages so I decided to move it to an external JavaScript file. According to this question and accepted answer...
Accessing ConfigurationManager.AppSettings in Java script
...external JavaScript files do not evaluate code inside of these "server-side code" tags, therefore, values from the web.config file must be passed from the .aspx page as parameters. I moved the function to an external JavaScript file and call the function like this:
<script src="Scripts/JavaScript.js" type="text/javascript">
var Value1 = "<%= System.Configuration.ConfigurationManager.AppSettings["Value1"].ToString()%>";
var Value2 = "<%= Session.Timeout %>";
externalFunction(Value1, Value2)
</script>
The external JavaScript functions begins as follows:
function externalFunction(Value1_, Value2_) {
debugger;
var Value1 = Value_1;
var Value2 = Value_2;
...
}
As I debug the JavaScript function, the arguments themselves are undefined. What am I missing here?
I have tried calling the function in both of the following ways.
<script src="Scripts/JavaScript.js" type="text/javascript">
var Value1 = "1";
var Value2 = "2";
externalFunction(Value1, Value2);
</script>
<script src="Scripts/JavaScript.js" type="text/javascript">
var Value1 = "1";
var Value2 = "2";
</script>
<script src="Scripts/JavaScript.js" type="text/javascript">
externalFunction(Value1, Value2);
</script>
Using IE's debugger, I can see that the values were pulled correctly from the web.config file, but the function still is not being called. I'm stumped.
Upvotes: 0
Views: 983
Reputation: 62290
Try with two separate script tags, and see any different.
<script src="Scripts/JavaScript.js"></script>
<script type="text/javascript">
var Value1 = "<%= System.Configuration.ConfigurationManager.AppSettings["Value1"].ToString()%>";
var Value2 = "<%= Session.Timeout %>";
externalFunction(Value1, Value2);
</script>
Upvotes: 2