简体   繁体   English

如何触发附加到页面的内联Javascript? (ASP.NET Web表单)

[英]How do I trigger inline Javascript that's appended to my page? (ASP.NET webforms)

I have a page section that is generated by the server, which includes inline javascript that is written by the server (This is ASP.NET webforms). 我有一个由服务器生成的页面部分,其中包括由服务器编写的内联javascript(这是ASP.NET Webforms)。 This javascript is used to bind an onchange event to my dropdown, which is in $(document).ready(). 这个javascript用于将onchange事件绑定到我的下拉列表中,该下拉列表位于$(document).ready()中。
When my page section is open on pageload, the $(document).ready() fires and the dropdown is bound correctly. 当页面加载时打开我的页面部分时,将触发$ {document).ready(),并且下拉列表已正确绑定。 However, when the section is not open on pageload (ie I have to click the 'expand' button, which contacts the server to build and load the page section), the inline script doesn't fire. 但是,如果在页面加载时未打开该部分(即我必须单击“扩展”按钮,该按钮与服务器联系以构建并加载页面部分),则不会触发内联脚本。
Basically, the difference is that in one case, the <script> tag is in the initial html that is loaded, and in the other case, the <script> tag is appended to the initial html after the page is loaded. 基本上,区别在于,在一种情况下, <script>标记位于已加载的初始html中,而在另一种情况下, <script>标记在页面加载后附加至初始html。

This is the inline script: 这是内联脚本:

<script type="text/javascript">
    alert('hi inline!');
    $j(document).ready( function(){ 
        var $emailNameHidden = $j('#hidden_TargetedEmailName');
        $emailNameHidden.on('change', function(){
            van.TargetedEmails.loadTestCaseNameSelect($emailNameHidden.val());
        });
     });
</script>

I added the alert to see if I could make it fire by putting it outside the $(document).ready(), but the alert also only fires when the script is included in the initial pageload. 我添加了警报以查看是否可以通过将其放置在$(document).ready()之外来触发,但是该警报也仅在脚本包含在初始页面加载中时才触发。

How do I fire the inline script once its been added to the page? 将内联脚本添加到页面后,如何触发它?

Sidenote: I tried putting this stuff in an external JS file that is included on the page section, but when I tried to bind my dropdown in the external file, the binding fired before the dropdown was finished loading so it didn't get bound, and when I put the binding in a $(document).ready(), the $(document).ready() didn't fire. 旁注:我尝试将这些内容放入页面部分中包含的外部JS文件中,但是当我尝试将下拉列表绑定到外部文件中时,该绑定在下拉列表加载完成之前触发,因此未绑定当我将绑定放入$(document).ready()时,没有触发$(document).ready()。

Just move your client-side logic to pageLoad event. 只需将客户端逻辑移动到pageLoad事件即可。

This JavaScript event is fired automatically by ASP.Net framework every time the page loads in the browser ieafter every post back ( ajax or non-ajax) and on initial load. 每当页面加载到浏览器中时,即每次回发(ajax或非ajax)之后以及初始加载时,ASP.Net框架都会自动触发此JavaScript事件。 Document ready event from jquery does not fire when you make an ajax post back in ASP.Net, but pageLoad event fires even when an ajax post back is made. 当您在ASP.Net中进行Ajax发布时,不会触发来自jquery的文档就绪事件,但是即使进行了Ajax发布,也将触发pageLoad事件。

function pageLoad() {
//put the document.ready logic here

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在“旧”ASP.NET Framework WebForms 基础架构中实现 Google 的 reCAPTCHAv3? - How Do I Implement Google's reCAPTCHAv3 in the “Old” ASP.NET Framework WebForms Infrastructure? 如何从Javascript在ASP.Net页面上调用静态方法? - How do I call a static method on my ASP.Net page, from Javascript? 在ASP.NET Webforms中,将UserControl添加到页面时如何运行JavaScript - In ASP.NET Webforms how to run JavaScript when adding a UserControl to a page 在asp.net webforms中引用javascript文件 - Referencing javascript files in asp.net webforms ASP.NET WebForms路由Javascript错误 - ASP.NET WebForms Routing Javascript Error 没有javascript的ASP.NET webforms - ASP.NET webforms without javascript 我可以使用ASP.NET WebForms母版页为每个内容页面包含不同的javascript / css文件吗? - Can I Include different javascript/css files per Content Page using ASP.NET WebForms Master Pages? 如何使用 javascript 触发 asp.net customvalidator - How to trigger asp.net customvalidator with javascript 我如何确保我的asp.net验证程序在我致电客户端JavaScript之前触发 - How do i ensure my asp.net validators fire before i call client side javascript 如何确定在调用动态javascript文件的页面与ASP.net中的用户之间 - How do I determine between a page calling a dynamic javascript file and a user in ASP.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM