nks
nks

Reputation: 115

Unable to read config file setting using javascript

I need to read the config file setting in javascript. I wrote the below code in my aspx page. It's returning empty.

<script type="text/javascript" language="javascript">

function GetFileLocationFromConfig(keyP) {

 var FileLocationL = '<%=ConfigurationManager.AppSettings[' + keyP+ '] %>';

 return FileLocationL;
            }
 </script>

Upvotes: 2

Views: 413

Answers (2)

skparwal
skparwal

Reputation: 1084

You cannot pass javascript variable to PHP, ASP variables / methods.

You can do like this:

<?php

$arr = implode(",", ConfigurationManager.AppSettings);

?>

var s = '<?php echo $arr; ?>';
s = s.split(',');
var FileLocationL = s[ keyP ];

Upvotes: 0

6502
6502

Reputation: 114559

You are confusing server side with client side.

The page is processed on the server and <% ... %> stuff is replaced with the result of computation serv side, then the generated page is sent to the client.

Part of the page computed can be Javascript code, but you must understand and discern what computation is done in Javascript on the client and what computation is instead done on the server by ASP.

In you specific case a solution would be writing ASP code that generates a Javascript "dictionary" object for example producing something like

 var settings = {};
 settings["!key1"] = "value1";
 settings["!key2"] = "value2";
 settings["!key3"] = "value3";

then the lookup function can be implemented in Javascript as

 function getSettingsValue(key) {
     return settings["!" + key];
 }

Be careful in knowing and understading exactly what you are sending to the client side by inspecting the generated page. For example sending passwords or other security related infos to the client would be a bad idea.

Upvotes: 3

Related Questions