2

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>
2
  • 1
    You cannot simply read files from javascript. You need to use File API: w3.org/TR/FileAPI Commented Jun 14, 2013 at 5:24
  • As a side-note, FileAPI works only on the latest versions of Chrome, FX and IE, it's not supported on IE9 and below. ActiveX will help (but only if your application is on an intranet) Commented Jun 14, 2013 at 5:26

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

very well explained... bravo
0

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 ];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.