#"/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval">eval(). It may also reduceaccessibility because it deviates from normal link behavior.
JavaScript URLs, URLs prefixed with the The JavaScript code to execute. The code will be parsed as a script. Note:Some other contexts that use URLs, such as the When a browser attempts to navigate to such a location, it parses and executes the script body. The script may have acompletion value (not a return value), which is the same value if the script were executed with Because In this example, the Note that because Instead of doing this, consider listening for the form's#"#syntax">SyntaxSyntax
#"#javascript">#"#script"><script>Description
#"/en-US/docs/Web/HTML/Reference/Elements/a#href">href attribute of an<a> or<area> element.action attribute of a<form> element.src attribute of an<iframe> element.window.location JavaScript property.href attribute of<link> elements, do not allow#"/en-US/docs/Web/URI/Reference/Schemes/data">data: URLs with thetext/javascript MIME type.eval(). If the last statement is anexpression, the completion value is the value of that expression. If this completion value is a string, that string is treated as an HTML document and the browser navigates to a new document with that content, using the same URL as the current page. No history entry is created. If the completion value is not a string, the browser only executes the code and does not navigate. Therefore, it's often recommended that if the script ends with a function call like#"/en-US/docs/Web/JavaScript/Reference/Operators/void">void to prevent accidental navigation if the function happens to return a string.#"/en-US/docs/Web/HTTP/Guides/CSP">content security policy settings, in particularscript-src.Examples
>Using
#"#">Click me</a>alert() returnsundefined, the browser does not navigate to a new page. This is a bad practice because the link is actually not a hyperlink. Consider making it a button instead:<button>Click me</button><script> document.getElementById("btn").addEventListener("click", () => { alert("Hello, world!"); });</script>href attribute of an<a> element is set to a#"Hello, world!":<a href="#">Click me</a><script> // Use a var so it becomes a global variable and can be read elsewhere var pageContent = "Hello, world!";</script>#"using_javascript_urls_as_form_actions" >Using
#"#"> <input /> <input type="submit" value="Submit" /></form>submit event and handling it with #"submit" value="Submit" /></form><script> document.getElementById("myForm").addEventListener("submit", (event) => { event.preventDefault(); alert(document.getElementById("myInput").value); });</script>