Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7928983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:54:59+00:00 2026-06-03T19:54:59+00:00

Why when i load page it runs function and alerts me function run i

  • 0

Why when i load page it runs function and alerts me “function run” i did not call it nowhere i need function to only trigger on element click.

<script type="text/javascript">

open_close = function() {
   alert("function run");
   //some code ...
}

window.onload = function() {
   //some code ...
   var myButton = document.getElementById('button');
   myButton.onclick = open_close();
   //some code ...
}

</script>

Here’s jfiddle demo http://jsfiddle.net/ayeBP/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T19:55:01+00:00Added an answer on June 3, 2026 at 7:55 pm

    Ah, but you did run it:

    myButton.onclick = open_close();
    

    Parentheses invoke a function. Use this instead:

    myButton.onclick = open_close;
    

    Better still (click through for legacy IE support):

    myButton.addEventListener('click', open_close);
    

    Okay this was simplidied function i need to actually pass 2 variables to it e.g. myButton.onclick = open_close(var1,var2);

    You still cannot use parentheses as you keep trying to do, because parentheses invoke the function. Use an anonymous function:

    myButton.onclick = function () {
        open_close(var1, var2);
    };
    
    // or, even better,
    myButton.addEventListener('click', function () {
        open_close(var1, var2);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a button1_click() function which runs on page load,,now i want to call
I need to have a function run once when the page is loaded, and
I have the following function that I want to run on page load and
If I load a page in an iframe, run doc.querySelect to retrieve a node
I'm running a click trigger when the page loads to show an iframe of
I'm trying to write an async page in asp .net which runs a function
I have a click function which runs code similar to the following: $('#go').click(function(e){ var
problem page: www.hovaness.com/work I have a function with variable name sliderun(); that runs a
When i am calling my image resize function within the page it runs smoothly
I have page that has some javascript that needs to run at page load.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.