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 1105875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:42:13+00:00 2026-05-17T01:42:13+00:00

Is there a reason why I should call a JavaScript method as follows? onClick=Javascript:MyMethod();

  • 0

Is there a reason why I should call a JavaScript method as follows?

onClick="Javascript:MyMethod();"

Or can I just call it like this:

onClick="MyMethod();"

Is there any difference?

  • 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-05-17T01:42:14+00:00Added an answer on May 17, 2026 at 1:42 am

    The value of an onclick attribute is code, not a URI, so this is correct (though not the only way you might do it, see tangent #1 below):

    onClick="MyMethod();"
    

    This is incorrect but largely harmless:

    onClick="Javascript:MyMethod();"
    

    Sometimes people think the latter is using the javascript protocol, like on links, but it isn’t. It’s doing something else entirely. The language of the code in the onclick attribute is defined at page level (and defaults to JavaScript), and so what you’re actually doing is declaring a label in your JavaScript code, and then calling MyMethod. JavaScript has labels (see tangent #2 below), though they’re not used much.

    The onclick attribute is totally different from the href attribute on links:

    <a href="javascript:MyMethod();">
    

    There, since we’re putting code where a URI is expected, we have to specify a URI using the javascript protocol, so the browser knows what we’re doing. javascript is a protocol (like http or mailto) that Brendan Eich (creator of JavaScript) was clever enough to define and register (and implement) very, very early on so it’s well-supported.

    Finally: Best to make onclick all lower case, not mixed case, although it only really matters if you use XHTML.


    Tangent #1

    Perhaps a bit off-topic, but: Using the HTML attributes for hooking up handlers is perfectly valid and works well cross-browser, but it intermixes your JavaScript event hookup with your HTML. Some people see that as a good thing, others belong to the “unobtrusive JavaScript” side and think you should hook everything up later. What you do is up to you. The unobtrusive approach is particularly useful when your HTML designers and your JavaScript coders are not the same people (as happens frequently on large teams).

    The unobtrusive approach basically says: Don’t use the HTML attributes for this, do it later from script. So instead of

    <ul id='tabset'>
        <li onclick="setTab(1);">Tab 1</li>
        <li onclick="setTab(2);">Tab 2</li>
        <li onclick="setTab(3);">Tab 3</li>
    </ul>
    

    you might have this HTML:

    <ul id='tabset'>
        <li>Tab 1</li>
        <li>Tab 2</li>
        <li>Tab 3</li>
    </ul>
    

    combined with this JavaScript:

    function hookUpTabs() {
        var tabset, tab;
        tabset = document.getElementById('tabset');
        for (tab = tabset.firstChild; tab; tab = tab.nextSibling) {
            if (tab.tagName == "LI") {
                tab.onclick = setTab; // Hooks up handler, but there are better ways
            }
        }
    }
    

    …where setTab uses context to figure out which tab was clicked and act accordingly, and hookUpTabs is called as soon as the DOM is ready. Note that where we’re setting up the click handler, we’re assigning a function reference, not a string, to the onclick on the tab div.

    I wouldn’t actually use onclick in the above, I’d use DOM2 handlers via the addEventListener (standard) / attachEvent (Microsoft) functions. But I didn’t want to get into standard vs. Microsoft stuff. And you don’t either, if you start doing unobtrusive JavaScript, use a library to handle that stuff for you (jQuery, Prototype, Closure, whatever).


    Tangent #2

    Another mildly off-topic tangent: So, what are these JavaScript labels then? Details in the spec as always, but here’s an example of using labels with a directed break statement in a loop:

    var innerIndex, outerIndex;
    
    // Label the beginning of the outer loop with the (creative) label "outerloop"
    outerloop: for (outerIndex = 0; outerIndex < 10; ++outerIndex) {
    
        for (innerIndex = 0; innerIndex < 50; ++innerIndex) {
    
            if (innerIndex > 3) {
                break; // Non-directed break, breaks inner loop
            }
    
            if (innerIndex > 2 && outerIndex > 1) {
                // Directed break, telling the code that we want to break
                // out of the inner loop *and* the outer loop both.
                break outerloop;
            }
    
            display(outerIndex + ":" + innerIndex);
        }
    }
    

    Live Example

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 537k
  • Answers 537k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If the result of node->GetName() does not contain the string… May 17, 2026 at 1:42 am
  • Editorial Team
    Editorial Team added an answer There's no automatic way to do this through a view… May 17, 2026 at 1:42 am
  • Editorial Team
    Editorial Team added an answer Are you running the application server as a service? Drive… May 17, 2026 at 1:42 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

In the Javascript for a Firefox extension, you can call gBrowser.getBrowserForTab but there is
I have a need for generating JavaScript on the server. I can do this
I would like to call a method which could potentially take on different versions,
The reason for a second sentence in my question as it can hold any
I know there is a lot of similar questions are tons of great answers
can I programatically (or, as we're speaking about html and css , semantically) decide
I have a unique situation where I'm building a site that will call data
I have a Drupal view which should output a video player using flash. I
I wonder how could I implement an asynchronous call in standard C++. I have
I have two SWF files which I shall call container and slave . The

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.