What is <a href="javascript:void(0)" > in HTML?

➊ Why is it used?

❑ When we click on a link, if we do not want the default behaviour of the browser to load a new page or refresh the same page.

❒ Instead, the browser performs the JavaScript attached to that link.
➋ Let's break it down

✛ javascript:
This is referred to as a Pseudo URL

✛ void
This is a JavaScript Operator

(0)
This is a JavaScript Expression
➋.➊ javascript:

❑ If "javascript:" is at the beginning of the value of "href" on <a> tag, it executes the JS code that follows the ":"

❒ The return value of the executed script "if there is one", becomes the content of a new document which is displayed in the browser
➋.➋ void

⬒ When an input is passed to "void", it evaluates that first. And, in all cases returns "undefined".

⬔ In other words, it suppresses the output of an expression.

⬓ It is useful when we want an expression to be evaluated without any side effects.
➋.➌ javascript:void(0)

⬒ The JavaScript code that will be executed after clicking the link is "void(0)"

⬔ "void" will first evaluate (0). (0) returns 0, but "void" suppresses it and returns "undefined"

⬓ Seeing "href" as "undefined", browser doesn't do anything
➌ Alternative

❑ When user disables "JavaScript to be executed in a browser", the above trick won't work.

❒ The below technique is the alternative and safest option.

<a href="#" onclick="return false;">
➍ Other most used Pseudo URLs

Like "javascript:", we can use below in "href"

✪ mailto:
To send email to an Email Address

✪ tel:
To call a telephone number
⚠️ Warning ⚠️

❑ javascript:void(0) violates Content Security Policy on CSP-enabled HTTPS

❒ Content Security Policy (CSP) is an added layer of security that helps to detect and mitigate certain types of attacks, including Cross-Site Scripting (XSS) and data injection attacks
⚠️ Warning ⚠️

❑ Anchor elements are often abused as fake buttons by setting their href to # or javascript:void(0) to prevent the page from refreshing, then listening for their click events.

❒ If possible, use a <button> instead.

❄ Reference: developer.mozilla.org/en-US/docs/Web…
➏ Let's summarise

❑ <a href="javascript:void(0)" > is used to stop browser to load a new page or refresh the same page.

❒ javascript:void(0) violates Content Security Policy on CSP-enabled HTTPS. Its usage should be avoided.

❑ If possible, use a <button> instead.
With this we come to the end of this 🧵 where we discussed about <a href="javascript:void(0)">

If you don't want to miss such tutorial threads on HTML, CSS and, JavaScript, follow {@swapnakpanda}

Till we meet again, stay safe and stay sanitised. 👋

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Swapna Kumar Panda ✨

Swapna Kumar Panda ✨ Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @swapnakpanda

16 Jan
What is < onclick=" return false;" > in HTML?

➊ Why is it used?

❑ Events automatically lead to certain actions performed by the browser.

❒ In some cases, we may not want default action to be performed.

❑ "return false" triggers browser to prevent taking default action.
➋ Let's break it down

✛ onclick
It's an HTML attribute.
It's being used to set inline event handlers.

✛ =""
The code written inside "" should be valid JS code.

✛ return false
Returning false from an inline event handler
Read 8 tweets
16 Jan
💙 10K+ Followers
📅 15-JAN

1K ⇨ First 3 months
10K ⇨ Next 3 months

Though I have been sharing the best possible learning contents, the kind of love and support I have received from you all is beyond my words.

You will receive even better contents, that's my promise

To never pollute my timeline, I rarely share any non-educational tweets.

Today I thought of sharing few single-liners that I wrote before but never able to post them.

Please read and share your views.
Just because a 12 yo doing Web Dev, don't mistaken it as a child's stuff.
Read 10 tweets
10 Jan
While "2" - 2 = 0, why is "2" + 2 = "22"?

A JavaScript 🧵
➊ '+' operator is overloaded for Strings

❑ The binary '+' operator is generally a "Number Addition" operator.

❒ When one of the operand is string, '+' rather acts like a "String Concatenation" operator. The other operand if not string is converted to string first.
➋ '-' operator operates on numbers only

❑ The binary "-" operator is known as a "Number Subtraction" operator.

❒ If any of the operand is not number, that is first converted to a number before evaluating the expression.
Read 8 tweets
4 Jan
Why <!DOCTYPE html>?

➊ History

✪ In the old days of the web, pages were written in two versions: One for Netscape Navigator, and one for Microsoft IE.

✪ When the web standards were made at W3C, browsers could not just start using them, as doing so would break most existing sites on the web.
✪ Browsers therefore introduced two modes to treat new standards compliant sites differently from old legacy sites.

✪ Modes are: Quirks Mode and Standards Mode.

✪ For HTML documents, browsers use a DOCTYPE in the beginning of the document to decide which mode to handle.
Read 9 tweets
3 Jan
How do you write it in HTML?

⇥ <img>
⇥ <img />
⇥ <img></img>

Which one should you use?

➊ Element Structure in HTML

⮑ Start Tag
Example: <div>
⮑ Content
Anything that's put between start and end tag
⮑ End Tag
Example: </div>
➋ Self Closing Tags

⬔ If you put a "/" before closing of the start tag, it's called a "Self Closing Tag".

Example: <img />

⬕ For a self closing tag, there won't be any content and, end tag.

Example: <img />abc</img> ✘
Read 7 tweets
1 Jan
Why to use <meta charset="utf-8"> in HTML?

➊ What is it?

Let's break down.

✛ meta: It's a HTML tag that contains metadata about a web page.

✛ charset: An HTML attribute that defines the character encoding for your browser to use when displaying the website content.

✛ utf-8: It's a specific character encoding.
➋ Why is character encoding important?

✪ Most websites these days has to display characters which are not traditional ASCII characters.

✪ When received such characters in HTML source, Browsers should be able to handle those to display properly.
Read 7 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(