From an extrnal Javascript file, I need to check for IsPostBack
(ASP.NET page). Here is what I found after googling:
var isPostBack = <%= Page.IsPostBack ? "true" : "false" %>;
But <%=
%>
doesn't seem to be recognized in the external JS file. If so, what is the alternate solution?
From an extrnal Javascript file, I need to check for IsPostBack
(ASP.NET page). Here is what I found after googling:
var isPostBack = <%= Page.IsPostBack ? "true" : "false" %>;
But <%=
%>
doesn't seem to be recognized in the external JS file. If so, what is the alternate solution?
5 Answers
Reset to default 3You won't be able to do that from an external file. Even if you could, external files get cached on the client's browser, and they don't get pulled every time. You might be able to place a function in the page and call it from the external script.
External Script
if(isAPostBack)
{
//run code
}
ASPX Page (Script in header)
var isAPostBack = <%= Page.IsPostBack %>;
Just make sure that your external script gets loaded after the above line in the page.
For ASP code to be processed you need to have a file extension which is mapped to the ASP dll in IIS.
The simplest case here would be to rename your .js
file with a .aspx
extension, then change the src attribute of your <script>
element.
Alternatively, create your isPostBack
variable globally in your aspx
page, and then call your js
file which contains its usage, eg:
<script type="text/javascript">
var isPostBack = <%= Page.IsPostBack ? "true" : "false" %>;
</script>
<script type="text/javascript" src="/js/myscript.js"></script> <!-- <- script that uses isPostBack -->
You cannot use <%= %>
notation in an external JavaScript file; it will not work.
This will not work in a Javascript file cause the Server will serve it as such and wont recognize the asp tags. You could do this, however, from a aspx file and it will work. You could also use a Generic Handler.
Good luck!
You can only render this variable
through the aspx page or user control or master page used on the page.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745470502a4629109.html
评论列表(0条)