检查外部 Javascript 文件中的 IsPostBack

发布于 2025-01-02 11:46:19 字数 257 浏览 6 评论 0原文

我需要从外部 Javascript 文件检查 IsPostBack (ASP.NET 页面)。这是我在谷歌搜索后发现的:

var isPostBack = <%= Page.IsPostBack ? "true" : "false" %>;

但是 <%= %> 似乎在外部 JS 文件中无法被识别。如果是这样,替代解决方案是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧情别恋 2025-01-09 11:46:20

这在 Javascript 文件中不起作用,因为服务器将照此提供服务并且无法识别 asp 标签。不过,您可以从 aspx 文件执行此操作,并且它会起作用。您还可以使用通用处理程序。

祝你好运!

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!

浪漫之都 2025-01-09 11:46:20

您只能通过页面上使用的 aspx 页面或用户控件或母版页来呈现此变量

You can only render this variable through the aspx page or user control or master page used on the page.

清醇 2025-01-09 11:46:19

您将无法从外部文件执行此操作。即使可以,外部文件也会缓存在客户端的浏览器上,并且不会每次都被提取。您也许可以在页面中放置一个函数并从外部脚本调用它。

外部脚本

if(isAPostBack)
{
//run code
}

ASPX 页面(标头中的脚本)

var isAPostBack = <%= Page.IsPostBack %>;

只需确保您的外部脚本在页面中上述行之后加载即可。

You 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.

小猫一只 2025-01-09 11:46:19

对于要处理的 ASP 代码,您需要有一个映射到 IIS 中的 ASP dll 的文件扩展名。

这里最简单的情况是使用 .aspx 扩展名重命名 .js 文件,然后更改

或者,在 aspx 页面中全局创建 isPostBack 变量,然后调用包含其用法的 js 文件,例如:

<script type="text/javascript">
    var isPostBack = <%= Page.IsPostBack ? "true" : "false" %>;
</script>
<script type="text/javascript" src="/js/myscript.js"></script> <!-- <- script that uses isPostBack -->

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 -->
又爬满兰若 2025-01-09 11:46:19

您不能在外部 JavaScript 文件中使用 <%= %> 表示法;这是行不通的。

You cannot use <%= %> notation in an external JavaScript file; it will not work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文