Javascript 页面加载后提醒

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10474829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:31:36  来源:igfitidea点击:

Alert after page load

javascripthtmlasp.net-mvc

提问by Chlebta

This is my script :

这是我的脚本:

<%
    if (TempData["Resultat"] != null){
%>
<script type="text/javascript">
    alert('<%: TempData["Resultat"]%>');
</script>
<%
    }
%>

In this case pop-up is shown before page loaded, but i want that's appear after page is fully loaded. in Html it's looks like this :

在这种情况下,弹出窗口在页面加载之前显示,但我希望在页面完全加载后出现。在 Html 中它看起来像这样:

<body onload="happycode() ;">

but i can't use it in MVC i got one master page for all my web application

但我不能在 MVC 中使用它,我的所有 Web 应用程序都有一个母版页

回答by Chase

If you can use jquery then you can put the alert inside the $(document).ready()function. it would look something like this:

如果您可以使用 jquery,那么您可以将警报放在$(document).ready()函数中。它看起来像这样:

<script>
  $(document).ready(function(){
    alert('<%: TempData["Resultat"]%>');
  });
</script>

To include jQuery, include the following in the <head>tag of your code:

要包含 jQuery,请在<head>代码的标记中包含以下内容:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>

Here's a quick example in jsFiddle: http://jsfiddle.net/ChaseWest/3AaAx/

这是 jsFiddle 中的一个快速示例:http: //jsfiddle.net/ChaseWest/3AaAx/

回答by Danilo Valente

There are three ways.
The first is to put the script tag on the bottom of the page:

有三种方式。
首先是将脚本标签放在页面底部:

<body>
<!--Body content-->
<script type="text/javascript">
alert('<%: TempData["Resultat"]%>');
</script>
</body>

The second way is to create an onload event:

第二种方式是创建一个onload事件:

<head>
<script type="text/javascript">
window.onload = function(){//window.addEventListener('load',function(){...}); (for Netscape) and window.attachEvent('onload',function(){...}); (for IE and Opera) also work
    alert('<%: TempData["Resultat"]%>');
}
</script>
</head>

It will execute a function when the window loads.
Finally, the third way is to create a readystatechangeevent and check the current document.readystate:

它将在窗口加载时执行一个函数。
最后,第三种方式是创建readystatechange事件并检查当前的document.readystate:

<head>
<script type="text/javascript">
document.onreadystatechange = function(){//window.addEventListener('readystatechange',function(){...}); (for Netscape) and window.attachEvent('onreadystatechange',function(){...}); (for IE and Opera) also work
    if(document.readyState=='loaded' || document.readyState=='complete')
        alert('<%: TempData["Resultat"]%>');
}
</script>
</head>

回答by user1378687

Why can't you use it in MVC?

为什么你不能在MVC中使用它?

Rather than using the body load method use jQuery and wait for the the document onready function to complete.

与其使用主体加载方法,不如使用 jQuery 并等待文档 onready 函数完成。

回答by Imp

With the use of jQuery to handle the document ready event,

使用jQuery处理文档就绪事件,

<script type="text/javascript">

function onLoadAlert() {
    alert('<%: TempData["Resultat"]%>');
}

$(document).ready(onLoadAlert);
</script>

Or, even simpler - put the <script>at the end of body, not in the head.

或者,甚至更简单 - 将 放在<script>的末尾body,而不是放在head.

回答by user1667495

$(window).on('load', function () {
 alert('Alert after page load');
        }
    });

回答by Max Pietrzak

Another option to resolve issue described in OP which I encountered on recent bootcamp training is using window.setTimeout to wrap around the code which is bothersome. My understanding is that it delays the execution of the function for the specified time period (500ms in this case), allowing enough time for the page to load. So, for example:

解决我在最近的训练营训练中遇到的 OP 中描述的问题的另一种选择是使用 window.setTimeout 来环绕代码,这很麻烦。我的理解是它会将函数的执行延迟指定的时间段(在本例中为 500 毫秒),从而为页面加载留出足够的时间。因此,例如:

<script type = "text/javascript">
            window.setTimeout(function(){
                alert("Hello World!");
            }, 500); 
</script>

回答by Subhash

Add the code below in the PageLoadEvent:

PageLoad事件中添加以下代码:

ScriptManager.RegisterStartupScript(Page, this.GetType(), "myScript", "alert('OK Done.');", true);

回答by Max Pietrzak

Another option is to use the deferattribute on the script, but it's only appropriate for external scripts with a srcattribute:

另一种选择是在脚本上使用defer属性,但它仅适用于具有src属性的外部脚本:

<script src = "exampleJsFile.js" defer> </script>