-1

aspx file

string firsrName="jafer";

myscript.js

GetMyName();

function GetMyName() {

    alert('<%=firstName%>');
}

I am not getting my value

Pete
  • 57,112
  • 28
  • 117
  • 166
jaffer
  • 23
  • 6
  • Is you JS file generated by some C# code or it is static file? – Alexei Levenkov Jan 04 '16 at 09:02
  • 1
    You would either have to call the function from the aspx file and pass the variable into the function, or you could create a js handler: http://stackoverflow.com/questions/10927185/dynamically-create-js-file-using-asp-net-handler – Pete Jan 06 '16 at 09:07

2 Answers2

1

The line alert('<%=firstName%>'); use the Web Form Page syntax. It is actually not possible to get the value like this because this syntax cannot be used in external JS files.

The simpliest (but not cleanest) method is to write the JS method into the layout file or another aspx file.

Read How to get asp.net client id at external javascript file

Community
  • 1
  • 1
ZwoRmi
  • 1,093
  • 11
  • 30
1

You could make a global variable in your aspx page and access it in your js using window.objectName

Reem
  • 150
  • 2
  • 15