Javascript HTML - 加载页面时的警报框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14884596/
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 18:13:20  来源:igfitidea点击:

HTML - Alert Box when loading page

javascripthtmlalertmessagebox

提问by Jonathan Gurebo

i'm using HTML code and i wan't to show un Alert Message or alert box, i don't know what it is called, but a message with a "OK" button.

我正在使用 HTML 代码,但不想显示警告消息或警告框,我不知道它叫什么,但显示带有“确定”按钮的消息。

i want to show this alert when the page is loaded.

我想在页面加载时显示此警报。

How do i do this?

我该怎么做呢?

And how to do this with a title in the Alert box?

以及如何使用警报框中的标题执行此操作?

Do I need JavaScript? if no, How to do this without javascript?

我需要 JavaScript 吗?如果没有,如何在没有 javascript 的情况下做到这一点?

Jonathan

乔纳森

回答by Adam Plocher

Yes you need javascript. The simplest way is to just put this at the bottom of your HTML page:

是的,你需要 javascript。最简单的方法是将其放在 HTML 页面的底部:

<script type="text/javascript">
alert("Hello world");
</script>

There are more preferred methods, like using jQuery's ready function, but this method will work.

有更多首选方法,比如使用 jQuery 的 ready 函数,但这种方法会奏效。

回答by Gash

You can use a variety of methods, one uses Javascript window.onload function in a simple function call from a script or from the body as in the solutions above, you can also use jQuery to do this but its just a modification of Javascript...Just add Jquery to your header by pasting

您可以使用多种方法,一种是在脚本或主体的简单函数调用中使用 Javascript window.onload 函数,如上面的解决方案,您也可以使用 jQuery 来执行此操作,但它只是对 Javascript 的修改。 . 只需通过粘贴将 Jquery 添加到您的标题中

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

to your head section and open another script tag where you display the alert when the DOM is ready i.e. `

到您的 head 部分并打开另一个脚本标签,当 DOM 准备好时,您会在其中显示警报,即`

<script>
    $("document").ready( function () {
        alert("Hello, world");
    }); 
</script>

`

`

This uses Jquery to run the function but since jQuery is a Javascript framework it contains Javascript code hence the Javascript alert function..hope this helps...

这使用 Jquery 来运行该功能,但由于 jQuery 是一个 Javascript 框架,它包含 Javascript 代码,因此 Javascript 警报功能..希望这有助于...

回答by Charlie Martin

you need a tiny bit of Javascript.

你需要一点点Javascript。

<script type="text/javascript">
window.onload = function(){ 
                alert("Hi there");
                }
</script>

This is only slightly different from Adam's answer. The effective difference is that this one alerts when the browser considers the page fully loaded, while Adam's alerts when the browser scans part the <script>tag in the text. The difference is with, for example, images, which may continue loading in parallel for a while.

这与亚当的回答略有不同。有效的区别在于,当浏览器认为页面已完全加载时,此警报会发出警报,而当浏览器扫描<script>文本中的部分标签时,Adam 会发出警报。不同之处在于,例如,图像可能会继续并行加载一段时间。

回答by Shanimal

If you use jqueryui (or another toolset) this is the way you do it

如果您使用 jqueryui(或其他工具集),这就是您的方式

http://codepen.io/anon/pen/jeLhJ

http://codepen.io/anon/pen/jeLhJ

html

html

<div id="hw" title="Empty the recycle bin?">The new way</div>

javascript

javascript

$('#hw').dialog({
    close:function(){
        alert('the old way')
    }
})

UPDATE : how to include jqueryui by pointing to cdn

更新:如何通过指向 cdn 来包含 jqueryui

<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.0.js"></script>
<script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>

回答by Alpesh

For making alert just put below javascript code in footer.

为了发出警报,只需在页脚中的 javascript 代码下方。

<script> 
 $(document).ready(function(){
    alert('Hi');
 });
</script>

You need to also load jquery min file. Please insert this script in header.

您还需要加载 jquery min 文件。请在标题中插入此脚本。

<script type='text/javascript' src='https://code.jquery.com/jquery-1.12.0.min.js'></script>

回答by vikas awasthi

<script type="text/javascript">
    window.alert("My name is George. Welcome!")
</script>

回答by Vi J

You can try this.

你可以试试这个。

$(document).ready(function(){      
  alert();
     $('#rep??ortVariablesTable tbody').append( '<tr><td>lll</td><td>lll</td></tr>');
}); 

回答by user5197904

<script> 
    $(document).ready(function(){
        alert('Hi');
    });
</script>