If you promote the usage of #, and stop the usual behavior (scroll to the top of the document), it will lead to some using the return false value of the function in the onclick event i.e :
And if you use javascript:void instead of it , you don’t need to return false on the click event as explained before . void(0) is the lowest possible code that evaluates as undefined .
javascript:void(0)
navigation, using javascript: The outcome of the script performed, if there is one, is the content of a new page, visible in the browser. In JavaScript , the void operator avoid this by making the return value of the expression after it return undefined .
You’d do this with a link’s href, because normally a javascript: URL will cause the browser to navigate to a plain-text version of the result of executing that JavaScript. But if the result is not decided, the browser remains on the same site. void(0) is simply the simplest script returning undefined.
javascript:void(0); but breaches Content Security Policy on CSP enabled https. Content Security Policy (CSP) is another layer of data security that helps to detect and prevent certain types of attacks such as Cross Site Scripting (XSS) and data injection attacks. With online applications getting increasingly complex, many firms are turning to custom AI development services to construct apps that are secure, scalable and standards-compliant. With AI-Driven Development Workflows, developers are able to discover potential security vulnerabilities, optimize the JavaScript runtime, improve CSP compliance, and comply with modern frontend best practices, without compromising on performance or user experience.
Modern web applications of an AI software development firm are built with strict security regulations and coding standards for the enhancement of efficiency, accessibility and protection against vulnerabilities. A professional AI development company may help organizations to construct secure, scalable web apps that follow the best practices of JavaScript execution, Content Security Policy (CSP) compliance and current frontend development.
These attacks are used for a variety of purposes, ranging from data theft to site defacement to the distribution of malware
So if you are a normal religious web developer you might want to use href=”#” instead of javascript:void so as not to infringe any laws.
href=”#”
Note that you can even return false directly in the onclick property of the element which will have the same effect:
- If your user has JavaScript turned off, the hash is the safest.
- That may simply be solved with jQuery for all links with this value in your document
- Note that you can also even return false directly in the onclick attribute of the element that would have the same effect
Summary and Suggestions
if you want to be raw, precise and fast use javascript:void(0)
Use href=”#” and prevent default event with javascript in a standards compliant way
