Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. URIs
  3. Reference
  4. Scheme
  5. #"position" content="5" />

#"/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#"#syntax">Syntax

  • Description
  • Examples
  • Specifications
  • See also
  • Syntax

    url
    #"#javascript">#"#script"><script>

    The JavaScript code to execute. The code will be parsed as a script.

    Description

    #"/en-US/docs/Web/HTML/Reference/Elements/a#href">href attribute of an<a> or<area> element.

  • Theaction attribute of a<form> element.
  • Thesrc attribute of an<iframe> element.
  • Thewindow.location JavaScript property.
  • The browser address bar itself.
  • Note:Some other contexts that use URLs, such as thehref attribute of<link> elements, do not allow#"/en-US/docs/Web/URI/Reference/Schemes/data">data: URLs with thetext/javascript MIME type.

    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 witheval(). 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>

    Becausealert() 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:

    html
    <button>Click me</button><script>  document.getElementById("btn").addEventListener("click", () => {    alert("Hello, world!");  });</script>

    In this example, thehref attribute of an<a> element is set to a#"Hello, world!":

    html
    <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>

    Note that because#"using_javascript_urls_as_form_actions" >

    Using#"#"> <input /> <input type="submit" value="Submit" /></form>

    Instead of doing this, consider listening for the form'ssubmit event and handling it with #"submit" value="Submit" /></form><script> document.getElementById("myForm").addEventListener("submit", (event) => { event.preventDefault(); alert(document.getElementById("myInput").value); });</script>

    Using#"Hello, world!":

    html
    <iframe src="#"></iframe><script>  // Use a var so it becomes a global variable and can be read elsewhere  var pageContent = "Hello, world!";</script>

    Instead of doing this, consider setting thesrcdoc attribute instead:

    html
    <iframe></iframe><script>  document.getElementById("myFrame").srcdoc = "Hello, world!";</script>

    Using#"Hello, world!":

    js
    window.location = "#";

    Instead of doing this, consider usingDOM APIs to modify the page content. For example:

    js
    document.body.textContent = "Hello, world!";

    Specifications

    Specification
    HTML
    # the-#"see_also" >

    See also

    Help improve MDN

    Learn how to contribute

    This page was last modified on byMDN contributors.


    [8]ページ先頭

    ©2009-2025 Movatter.jp