admin管理员组文章数量:1426026
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.
本文标签: aspnetCheck for IsPostBack in external Javascript fileStack Overflow
版权声明:本文标题:asp.net - Check for IsPostBack in external Javascript file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745470502a2659725.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论