javascript 在没有onclick的情况下加载页面时调用javascript函数

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

Calling javascript function when page loaded without onclick

phpjavascripthtml

提问by Dim

I have an footer.php file that have javascript code and functions. I use this footer.php file for all of my pages. When clicking buttons I use

我有一个包含 javascript 代码和函数的 footer.php 文件。我将这个footer.php 文件用于我的所有页面。单击我使用的按钮时

onclick="MyFunc()"

but in some pages I need to call MyFunc without onclick, just when page loaded. I can not use "body onload" function because the tag is in header.php file that all pages is using it. I have triend to put

但在某些页面中,我需要在没有 onclick 的情况下调用 MyFunc,就在页面加载时。我无法使用“body onload”功能,因为该标签位于所有页面都在使用的 header.php 文件中。我想把

<script Language="JavaScript">
window.onload=MyFunc; 
</script>

in my php file and

在我的 php 文件和

<script Language="JavaScript">
function MyFunc(){
alert('hello');
}
</script>

in the footer.php but with no luck.

在 footer.php 中,但没有运气。

回答by Broxzier

"but in some pages I need to call MyFunc without onclick"

“但在某些页面中,我需要在没有 onclick 的情况下调用 MyFunc”

What I understand from your question is:

我从你的问题中了解到:

  • You want to fire MyFuncon a few pages
  • All pages include footer.php(at the end, I assume).
  • MyFuncis already present
  • 你想MyFunc在几页上开火
  • 所有页面都包括footer.php(最后,我假设)。
  • MyFunc已经存在

If that's right, you'll have to trigger this event somehow only on the pages that you want it to fire. This can be done two ways.

如果这是对的,您将不得不仅在您希望它触发的页面上以某种方式触发此事件。这可以通过两种方式完成。

Add a small snippet to the automatically firing pages

向自动触发的页面添加一个小片段

window.addEventListener('load', MyFunc, false);

This code will add the MyFuncfunction to the array that will be fired when the page is laded. Remember: window.onloadand this will fire afterall images have been loaded.

此代码会将MyFunc函数添加到加载页面时将触发的数组。请记住:window.onload这将加载所有图像触发

Trigger MyFuncautomatically

MyFunc自动触发

I don't know what the difference between those pages is, but this could be done by firing the function when the page is loaded, just like the snippet I showed above, and just before this is done you check for a trigger. For example, if your page where it fires has an element with a specific class test, simply check if it exists (document.getElementsByClassName('test').length > 0) and then fire MyFunc.

我不知道这些页面之间的区别是什么,但这可以通过在页面加载时触发函数来完成,就像我上面显示的代码段一样,在完成之前检查触发器。例如,如果触发的页面有一个具有特定 class 的元素test,只需检查它是否存在 ( document.getElementsByClassName('test').length > 0) 然后触发MyFunc

I hope this helps.

我希望这有帮助。

回答by Carl Markham

As stated you could use window.onloador you could attach the event to DOM load

如前所述,您可以使用window.onload或者您可以将事件附加到 DOM 加载

<body onload="MyFunc()"></body>

回答by Axidepuy

as I know the language attribute is obsolete try to use <script type="text/javascript">instead of <script Language="JavaScript">

据我所知语言属性已过时尝试使用<script type="text/javascript">而不是<script Language="JavaScript">

EDIT: put a sample to here jsFiddle

编辑:将示例放在这里jsFiddle

回答by Enve

if its in the footer (end of the page) you can simply enter this code where you need

如果它在页脚(页面末尾),您只需在需要的地方输入此代码

<script>
    MyFunc()
</script>

this will call the function

这将调用函数