DO YOU NEED A CONTENT WRITER FOR YOUR BUSINESS?

Your One-Stop Solution for All Content Needs! Click here for more!
Html

Href attribute for links: “javascript:void(0)” or “#”?

Share

“javascript:void(0)” or “#”, these are two type by which we can call a JavaScript code. But, what do you prefer to do most?

These are for a building a link that has the sole purpose of running JavaScript code. But, in terms of functionality, page loading speed, validation purposes, etc. It may come to our mind that which is better.

For example, look at these below code:

<a href="#" >
<a  (0)" >
<a href="javascript:myFunction();">Click Here</a>

As, it does not cause problems but it’s a like the same as PreventDefault. While you are using “#” then, you will jump to the top of the page and the URL will append the anchor # as well.
To avoid this, you can simply return false; or use javascript:void(0);

And, <a >{text-decoration:underline;} and you will have “link look-like”.

The void operator is often used to obtain the undefined primitive value i.e. usually using “void(0)”. The reason you’d want to do this with the href of your link is that normally, a javascript: URL which will redirect to the browser to a plain text version of the result of evaluating that JavaScript.
But, if a result is undefined, then the browser stays on the same page as it was. void(0) is only the smallest script that is possible to evaluates as undefined.

But, note to remember that all that code was good back few years ago, though now it can be a bad practices now. Now, there are easier, better and more maintainable and scalable ways of accomplishing your desired result.

If you are planning to add other types of Javascript to your site, especially if your website is based on WordPress, you need to be careful to make sure you don’t make any mistakes and break your site. If you want more guidance on how to add scripts correctly, in this detailed article on how to add javascript to wordpress guide on CollectiveRay.com.

Namaste UI

For any types of queries, you can contact us on info[at]namasteui.com.

Recent Posts

Understanding Battery Coating: Key Factors to Perfection

Battery coating is the process of applying uniform layers of active materials—such as cathode and…

4 hours ago

Edge Computing vs Cloud Computing: What’s the Difference?

Let’s face it. Tech buzzwords get thrown around a lot—especially when it comes to how…

3 days ago

How Data Intelligence Shapes the Future of Digital Innovation

In today’s digital world, the boundaries between technology, finance, and innovation are rapidly disappearing. Businesses…

6 days ago

How To Power Your Backyard Parties with A Solar Power Battery?

Backyard gatherings like BBQs, family reunions, and garden parties are an exciting way to enjoy…

7 days ago

5 Best Practices for Programmatic Advertising

Marketers are always on the lookout for more effective ways to reach their target audiences.…

1 week ago

Are We All Addicted to Stimulation? The New Face of Anxiety Disorders

Does your phone control your mind more than you control your phone? Modern life exploits…

1 week ago