[英]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.