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
Calling javascript function when page loaded without onclick
提问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
MyFunc
on a few pages - All pages include
footer.php
(at the end, I assume). MyFunc
is 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 MyFunc
function to the array that will be fired when the page is laded. Remember: window.onload
and this will fire afterall images have been loaded.
此代码会将MyFunc
函数添加到加载页面时将触发的数组。请记住:window.onload
这将在加载所有图像后触发。
Trigger MyFunc
automatically
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.onload
or you could attach the event to DOM load
如前所述,您可以使用window.onload
或者您可以将事件附加到 DOM 加载
<body onload="MyFunc()"></body>
回答by Axidepuy
回答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
这将调用函数