php - Calling javascript function when page loaded without onclick - Stack Overflow

I have an footer.php file that have javascript code and functions. I use this footer.php file for all o

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

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

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

in my php file and

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

in the footer.php but with no luck.

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

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

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

in my php file and

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

in the footer.php but with no luck.

Share Improve this question edited Aug 19, 2013 at 13:05 Dim asked Aug 19, 2013 at 12:45 DimDim 4,84719 gold badges86 silver badges152 bronze badges 3
  • if you dont have problem with jquery, then you can use $('document').load(/*your function*/). – schnill Commented Aug 19, 2013 at 12:53
  • why you are not using jquery ?? $('document').ready() – Prince Singh Commented Aug 19, 2013 at 12:54
  • You could use jquery library and then $(window).load(function () { MyFunc(); }); – Shuhel Ahmed Commented Aug 19, 2013 at 12:55
Add a ment  | 

4 Answers 4

Reset to default 1

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

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

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 after all images have been loaded.

Trigger MyFunc automatically

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.

I hope this helps.

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

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

EDIT: put a sample to here jsFiddle

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745404852a4626269.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信