---
title: "How to access a backend variable in front end using javascript"  
description: "How to access a backend variable in front end using javascript"  
author: "Anonymous User"  
published: 2015-02-05  
updated: 2015-02-05  
canonical: https://www.mindstick.com/forum/12945/how-to-access-a-backend-variable-in-front-end-using-javascript  
category: "asp.net"  
tags: ["vb.net", "jquery", "javascript"]  
reading_time: 2 minutes  

---

# How to access a backend variable in front end using javascript

I had [Public](https://www.mindstick.com/blog/11097/java-access-modifiers-the-public-and-the-private-modifiers) declared Dictonary in [code behind](https://www.mindstick.com/forum/2199/call-javascript-s-function-from-code-behind) as :

```
 Public dics As New
Dictionary(Of String, String()) From { _{"picture", New String() {".jpeg", ".jpg",
".png", ".bmp", ".gif", ".tif"}}, _{"document", New String() {".doc", ".docx",
".txt", ".htm", ".html", ".xml", ".xaml",
".css"}}, _{"excel", New String() {".xls", ".xlsx",
".xlt", ".xla"}}, _{"pdf", New String() {".pdf"}}, _{"zip", New String() {".7z", ".APK",
".BAT", ".rar", ".dll", ".jar", ".zip"}},
_{"ppt", New String() {".ppt", ".pos",
".pps"}}}
```

**Edit :**

if i do like this

```
function myFunction() {       var dic = "<%= dics %>";       var array_keys =new Array();       var array_values = new Array();       for (var key in dic) {           alert(key);        }     }
```

How can i [access](https://www.mindstick.com/articles/12994/how-foreigners-can-access-blocked-websites-in-china) this Dictonary in [javascript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript) to do some [operations](https://www.mindstick.com/blog/304985/how-does-devops-bridge-the-gap-between-development-and-operations-teams-like-git)

## Replies

### Reply by Anonymous User

For now it looks like you need to serialize dictionary into javascript object and then paste it in your JavaScript. You could use any library for serialization. E.g. [Newtosoft.Json](https://www.nuget.org/packages/newtonsoft.json/). Like this:

```
function myFunction() {   var dic = <%= Newtonsoft.Json.JsonConvert.SerializeObject(dics) %>;   var array_keys = new Array();   var array_values = new Array();   for (var key in dic) {       alert(key);    } }
```

Note that I removed quotes.

But I suggest you to get rid of this approach and not mix JavaScript and ASP.Net [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii). So in my vision you should load this dictionary via AJAX or if it's not possible place ASP.Net in some other place. E.g.:

**View:**

```
<input type="hidden" id="dictionaryInput" value="<%=Newtonsoft.Json.JsonConvert.SerializeObject(dics)%> />
```

**JavaScript:**

```
function myFunction() {   var dicInput = document.getElementById('dictionaryInput');   var dic = JSON.parse(dicInput.value);   var array_keys = new Array();   var array_values = new Array();   for (var key in dic) {       alert(key);    } }
```

Hope it will help.


---

Original Source: https://www.mindstick.com/forum/12945/how-to-access-a-backend-variable-in-front-end-using-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
