2013-08-19 12:45
浏览 73


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


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">

in my php file and

<script Language="JavaScript">
function MyFunc(){

in the footer.php but with no luck.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • douan2907 2013-08-19 13:12

    "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.

    点赞 打赏 评论
  • douchun9719 2013-08-19 12:49

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

    <body onload="MyFunc()"></body>
    点赞 打赏 评论
  • dongpinken0498 2013-08-19 12:55

    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

    点赞 打赏 评论
  • dre93205 2013-08-19 12:56

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


    this will call the function

    点赞 打赏 评论

相关推荐 更多相似问题