[英]executing javascript function on page load
Right now, I have a javascript function which is triggered onclick. 现在,我有一个触发onclick的javascript函数。 However, I want the same function to be triggered when DOM is loaded. 但是,我希望在加载DOM时触发相同的功能。 Following code works, however, I don't want to put 'script' tag in the middle of the view. 但是,在代码工作之后,我不想在视图中间放置“script”标记。 Calling the function from the body tag is not an option here. 这里不能选择从body标签调用函数。
<script>document.addEventListener("DOMContentLoaded",function(){extractions.RefreshCheck(@check.ID)});</script>
Code snippet of where I want to implement this: 我要实现此目的的代码片段:
@foreach (var check in Model.FailedChecks)
{
<li class="@( check.IsOK ? Html.Raw("bg-success") : Html.Raw("bg-danger") ) " cid="@check.ID">
@Html.ActionLink(check.Display, "XList", "XList", new { filter = check.GetQuery(), Layout = check.Layout }, new { target = "_blank" });
<span class="glyphicon glyphicon-refresh" onclick="extractions.RefreshCheck(@check.ID);" onload="initAutoRefresh"></span>
@*<script>document.addEventListener("DOMContentLoaded",function(){extractions.RefreshCheck(@check.ID)});</script>*@
</li>
}
Above codes work, but I do not want that script tag in my view. 上面的代码可以工作,但我不希望在我的视图中使用该脚本标记。 So I tried to add the following code in my javascript file using 'onload' eventlistner and it does not work. 所以我尝试使用'onload'ventlistner在我的javascript文件中添加以下代码,但它不起作用。 I think this is the problem. 我认为这是问题所在。
@foreach (var check in Model.FailedChecks)
{
<li class="@( check.IsOK ? Html.Raw("bg-success") : Html.Raw("bg-danger") ) " cid="@check.ID">
@Html.ActionLink(check.Display, "XList", "XList", new { filter = check.GetQuery(), Layout = check.Layout }, new { target = "_blank" });
<span class="glyphicon glyphicon-refresh" onclick="extractions.RefreshCheck(@check.ID);" onload="extractions.InitAutoRefresh()"></span>
</li>
}
And my InitAutoRefresh function : 我的InitAutoRefresh函数:
var extractions= {
InitAutoRefresh: function () {
if (document.readyState === 'complete') {
RefreshCheck();
console.log("function already loaded in DOM")
} else {
document.addEventListener('DOMContentLoaded', function () {
RefreshCheck();
console.log("function loaded in dom");
});
}
},
RefreshCheck: function(intCheckId){
$('li[cid=' + intCheckId + ']').addClass('bold');
$.get(window.location + '/Home/UpdateIntegritycheck?checkId=' + intCheckId, function(data){
$('li[cid='+intCheckId+']').replaceWith(data);
});
}
}
Function RefreshCheck works fine on click (ie it updates record). 函数RefreshCheck在单击时工作正常(即它更新记录)。 I would be more than happy to get your feedbacks. 我很乐意收到您的反馈意见。 Thank you. 谢谢。
Try this code: 试试这段代码:
@section scripts{
<script>document.addEventListener("DOMContentLoaded", function () { xtractions.RefreshCheck(id) });</script>
}
if you are using jquery then you can use 如果您使用的是jquery,那么您可以使用
$(document).ready(function () {
your code here
});
One approach is to define custom attribute on your html tags on which you can fire conditionaly according to the tag value. 一种方法是在html标签上定义自定义属性,您可以根据标签值在其上触发条件。 Example : 示例:
<span data-click-on-dom-ready="true" onclick="extractions.RefreshCheck(@check.ID);" ></span>
Then far away from your partial view, you can put the following: 然后远离你的局部视图,你可以把以下内容:
$(document).ready(function () {
$("[data-click-on-dom-ready='true']").trigger('click');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.