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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:31:47+00:00 2026-06-08T13:31:47+00:00

Possible Duplicate: Do you ever need to specify javascript: in an onclick? To execute

  • 0

Possible Duplicate:
Do you ever need to specify javascript: in an onclick?

To execute JavaScript on a DOM event, you could use something like this:

<div onclick="alert('Alert');">Alert</div>​

Something like this seems to work as well:

<div onclick="javascript: alert('Alert');">Alert</div>​

However, I’ve heard that the second example is “bad” and that the first example should be used over the second.

Is this bad? If so, why is this bad? What is the difference between alert('Alert') and javascript: alert('Alert')?

What about using it in <a> tags (if it is any different)?

<a href="javascript: alert('Alert');">Alert</a>

Edit: To clarify, I am asking about the javascript: part specifically, and not how I have inline JavaScript mixed in with my markup. Sorry about the confusion.

  • 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-08T13:31:49+00:00Added an answer on June 8, 2026 at 1:31 pm

    Oh the wonderful confusing world of JavaScript. The code you posted probably doesn’t do what most programmers think it’s doing.

    There is a difference between each of the following lines:

    <a onclick="alert('Hello World!')"...>example</a> //V1
    <a href="javascript: alert('Hello World!')"...>example</a> //V2
    <a onclick="javascript: alert('Hello World')"...>example</a> //V3
    

    although they all will alert Hello World!.

    The first (V1) has an inline click event bound via the [onclick] attribute. It may also contain an [href] attribute that navigates to another location after the [onclick] attribute has executed, or any number of other click events bound in the code, assuming the default behavior hasn’t been prevented.

    The second (V2) has an executable javascript: url set as the [href] attribute. It might also contain an [onclick] attribute or other click events bound in external scripts.

    The first and second examples (V1 & V2) have identical code executed, which is:

    alert('Hello World!')
    

    The third example (V3) has an inline click event bound via the [onclick] attribute, just like V1, however the code being executed is different. The executed code is:

    javascript: alert('Hello World')
    

    Although it looks like a javascript: url, it’s actually just using a label in javascript.

    Labels in JavaScript are useful for skipping out of nested loops, as in the following example code:

    label: for (i = 0; i < 5; i++) { //labeled line
        for (j = 0; j < 5; j++) {
            console.log(i, j);
            if (i === 2 && j === 3) {
                break label; //this jumps out of both for loops
            }
        }
    }
    

    In most inline JavaScript, it’s misused because the author doesn’t understand the difference between the three formats.


    Why is using javascript: <code> bad?

    That’s a leading question. It assumes that using javascript: <code> is bad.

    javascript: <code> isn’t bad. It’s a tool. Tools aren’t inherently good or bad, only the people using the tools are. You wouldn’t call a hammer “bad”, even if someone used it as a weapon.

    javascript: <code> has some nice uses. You shouldn’t use it for most cases because it’s the wrong tool for the job, however if you’re writing a bookmarklet, you’d be required to use the javascript: <code> format.

    Additionally, there are a few niche contexts where it could make sense to leave javascript inline. An example of this would be to add a simple print button to the page:

    <a href="#" onclick="window.print(); return false">Print</a>
    

    Although even this example could be easily replaced by a class and externalizing the javascript:

    <a href="#" class="print">Print</a>
    <script>
         //jQuery used for brevity
         $(document).on('click', '.print', function () {
             window.print();
             return false;
         });
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Do you ever need to specify javascript: in an onclick? What is
Possible Duplicate: Can select * usage ever be justified? Curious to hear this from
Possible Duplicate: PHP Can a client ever set $_SESSION variables? What I'd like to
Possible Duplicate: How Random is System.Guid.NewGuid()? Based on this question I would like to
Possible Duplicate: Where are $_SESSION variables stored? When ever we working with session, we
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Is it ever ok to store password in plain text in a
Possible Duplicate: Why would you ever want to allocate memory on the heap rather

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.