javascript

javascript: URL

[36] javascript: URL は、 Webブラウザーによって JavaScript として解釈され、実行される URL です。

仕様書

構文

[60] javascript: URL は、 URL scheme である javascript と区切りの : の後に JavaScriptソースコードを指定するものです。 ただし、ソースコードの部分はパーセント符号化を使うことができます。

  1. javascript:
  2. パーセント符号化された JavaScript コード

[61] URL query素片識別子は使うことができず、 ?# も含めてすべて JavaScript の一部と解釈されます >>67

[68] JavaScript コードの先頭の U+FEFF は、 BOM とみなされます >>67

[69] HTMLnavigate にはこれに対応する規定がありません。 JavaScript としては BOM でなくても U+FEFF は無視されるため、結果同じとなります。

navigate 演算

[21] URL schemejavascript:URL への navigate は、 指定された URL に含まれる JavaScript コードを実行して、 その結果を表示する操作となります。

[120] javascript: URL要求を実行 (execute a javascript: URL request) は、 要求要求閲覧文脈始点閲覧文脈閲覧文脈について、 次のようにします。 >>35

  1. [80] 応答を、 新しい 204 応答に設定します。
    [121] 応答
    状態
    204
  2. [24] 始点活性文書起源閲覧文脈活性文書起源同じ起源であって、 要求other始点閲覧文脈について Should navigation request of type from source in target be blocked by Content Security Policy?Allowed を返す場合、
    1. [94] URL文字列を、 要求URLURL直列化器を適用した結果に設定します。
    2. [122] URL文字列の先頭の javascript: を除去します。
    3. [123] スクリプトソースを、 URL文字列文字列パーセント復号を適用した結果を UTF-8復号した結果に設定します。
    4. [116] 閲覧文脈活性文書URLを、 要求URLリスト末尾に追加します。
    5. [85] 設定群を、 閲覧文脈活性文書関連設定群オブジェクトに設定します。
    6. [53] スクリプトを、 古典スクリプトを作成した結果に設定します。
      スクリプトソース
      スクリプトソース
      環境設定群オブジェクト
      設定群
      基底URL
      設定群API基底URL
      スクリプトfetchオプション群
      既定古典スクリプトfetchオプション群
    7. [54] 評価状態を、 スクリプトについて古典スクリプトを実行した結果に設定します。
    8. [104] 評価状態abrupt completion の場合、 または評価状態の場合、
      1. [105] 結果を、 未定義に設定します。
    9. [106] それ以外の場合、
      1. [107] 結果を、 評価状態に設定します。
    10. [55] Type(結果)String の場合、
      1. [81] 応答を、 新しい応答に設定します。
        [52] 応答
        ヘッダーリスト
        Content-Type
        text/html
        Referrer-Policy
        設定群参照元ポリシー
        本体
        結果
        HTTPS状態
        設定群HTTPS状態
  3. [56] 応答を返します。

[58] <a href> に指定された時はその閲覧文脈で評価されますし、 <iframe src> に指定された時はその iframe入れ子閲覧文脈で評価されます >>35

[59] 多くの Bookmarklet は、 navigate により他のページに遷移することを防ぐため、 最後に void() を実行するなどして void を返すようにしています。

[57] この手順は、 navigate から呼び出されます。 この手順が返した応答は、 navigate応答の処理に回されます。

[63] この手順は、他の URL scheme なら fetch に相当するものです。 スクリプトを実行するという特別な性質から、 javascript: URL から HTTP応答を取得する処理は fetch ではなく navigate の一部として規定されています。このため fetch が直接呼び出される場面 (例えば <img src>) では javascript: URL を使うことができません。歴史的にはそのような場面でも JavaScript を実行していた Webブラウザーも存在していました。

歴史

[1] 不思議な指定? javascript:'css〜'について http://www.parkcity.ne.jp/~chaichan/qanda/qa5032.htm

javascript: scheme を data:about: のように使うという話です。 (このスレ自体はレベルが低くて参考にはなりません。)

GeckoWinIE 6 も、 ブラウザのアドレス欄に javascript:"<p>abcdefg</p>" のように入れてみると、評価結果を HTML として処理することが分かります。 (がおそらくその辺は適宜判断で、件の掲示板のスレのように CSS として解釈させることもできるのでしょう。)

これ、使い方によっては security hole になりかねないので注意する必要がありそうです・・って、 javascript: scheme 自体 security hole だから問題は変わっていませんか。 (名無しさん 2004-05-09 03:54:16 +00:00)

[2] そういえば javascript: scheme の仕様はやっぱり存在していないのか? Netscape の仕様書くらいはあるのか?

[3] >>2 JavaScript 1.3 のマニュアルより:

[4] Additional Topics — Using JavaScript URLs http://devedge.netscape.com/library/manuals/2000/javascript/1.3/guide/advtopic.html#1004952

[5] JavaScript Security http://devedge.netscape.com/library/manuals/2000/javascript/1.3/guide/sec.html

[6] http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/location.html#1203002

URI scheme の説明。得られる情報は >>4 の subset。

[7] Displaying Errors with the JavaScript Console — Opening the JavaScript Console http://devedge.netscape.com/library/manuals/2000/javascript/1.3/guide/console.html#1044393

JavaScript 1.5 のマニュアルには javascript: URI についての言及はありませんでした。

[8] javascript Protocol (Internet Explorer - Asynchronous Pluggable Protocols) http://msdn.microsoft.com/workshop/networking/predefined/javascript.asp

[9] Web Forms 2.0>>3-7 の JavaScript 1.3 の説明書を javascript: の出典に選んでいます (DevEdge はなくなっちゃったけど)。 だから JS1.3 が一番まともな原典なのかも。

WHAT WG がいずれ javascript: の仕様も作ってくれると期待するのも一つの手(?)かも。 (名無しさん)

[10] 関連する URI scheme: ecmascript:, livescript:, mocha:, data: (名無しさん [sage])

[11] VRML における javascript: URI scheme の定義:

  1. VRML97, ISO/IEC 14772-1:1997 -- Annex C, ECMAScript Scripting Reference http://www.web3d.org/x3d/specifications/vrml/ISO-IEC-14772-IS-VRML97WithAmendment1/part1/javascript.html#Access
  2. VRML97, ISO/IEC 14772-1:1997 -- 4 Concepts http://www.web3d.org/x3d/specifications/vrml/ISO-IEC-14772-IS-VRML97WithAmendment1/part1/concepts.html#4.5.4

(名無しさん [sage])

[12] Extensible 3D (X3D), ISO/IEC 19775-1:200x, Part 1 -- 9 Networking component http://www.web3d.org/x3d/specifications/ISO-IEC-19775-IS-X3DAbstractSpecification/Part01/components/networking.html#ScriptingLanguageProtocols

>>11 X3D における定義。 javascript: は非推奨になり、代わりに ecmascript: が用意されています。 (名無しさん [sage])

[13] >>12 X3D Specification, ISO/IEC 19775-2:200x, Part 2, Annex A VRML97 Scripting Backwards Compatibility http://www.web3d.org/x3d/specifications/ISO-IEC-19775-IS-X3DAbstractSpecification/Part02/vrml97.html#InlineScriptDefinition

(名無しさん [sage])

[14] JavaScript in CSS files http://www.quirksmode.org/css/javascript.html (名無しさん)

[15] The 'javascript' resource identifier scheme urn:ietf:id:draft-hoehrmann-javascript-ri-00 http://www.websitedev.de/ietf/draft-hoehrmann-javascript-ri-00.txt http://ietfreport.isoc.org/idref/draft-hoehrmann-javascript-ri/

(名無しさん 2006-10-28 15:11:00 +00:00)

[16] The 'javascript' scheme from Bjoern Hoehrmann on 2006-10-28 (uri@w3.org from October 2006) http://www.w3.org/mid/nb66k29fqd915t8u7f3lsn6oj2ctj5og5t@hive.bjoern.hoehrmann.de

(名無しさん 2006-10-28 15:19:14 +00:00)

[17] The 'javascript' scheme from Bjoern Hoehrmann on 2006-10-28 (uri@w3.org from October 2006) http://www.w3.org/mid/nb66k29fqd915t8u7f3lsn6oj2ctj5og5t@hive.bjoern.hoehrmann.de

(名無しさん 2006-10-28 15:19:32 +00:00)

[18] crisp’s blog &#187; Blog Archive &#187; The useless javascript: pseudo-protocol http://therealcrisp.xs4all.nl/blog/2006/12/05/the-useless-javascript-pseudo-protocol/ (名無しさん 2006-12-18 23:05:31 +00:00)

[19] Firefox 1.5 の怪しい href="javascript:" 処理 (わかば 著, 2007-04-26 22:07:50 +09:00 版) https://suika.suikawiki.org/~wakaba/d/d200704#d7-1 (名無しさん 2007-06-03 14:38:45 +00:00)

[22] javascript: URI文字列を返した場合、 Firefox 1.5 は (aURI の直接指定なら) text/html と解釈しますが、 Opera 9 は現在の文書と同じ媒体型とみなすようです。

例えば、 text/xml 文書:

...
<a href="javascript:'a'">link</a>
...

... のリンクをたどると、 Firefoxa と表示しますが、 Opera整形式でないといってきます。

(名無しさん)

[23] seamonkey mozilla/dom/src/jsurl/ ( 版) http://mxr.mozilla.org/seamonkey/source/dom/src/jsurl/

[25] hoshikuzu | star_dust の書斎 - 2007-09-18 (2007-09-21 11:56:34 +09:00 版) http://d.hatena.ne.jp/hoshikuzu/20070918#p1 (名無しさん)

[26] javascript: URIs ( 版) https://suika.suikawiki.org/~wakaba/-temp/test/uri/javascript/test.cgi# (名無しさん)

[28] JavaScriptを記述、実行を促す動画について‐ニコニコニュース ( 版) http://blog.nicovideo.jp/niconews/2009/09/004642.html

[29] [whatwg] Javascript: URLs as element attributes ( 版) http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2010-November/029042.html

[30] Inline Images on Web Pages ( ( 版)) http://elf.org/essay/inline-image.html

[31] The whatwg December 2010 Archive by thread ( ( 版)) http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2010-December/thread.html

[32] [whatwg] Javascript: URLs as element attributes ( 版) http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030360.html

[33] Web Applications 1.0 r6139 Drop javascript: support from <img src>, <object data>, CSS, etc, etc, etc. ( ( 版)) http://html5.org/tools/web-apps-tracker?from=6138&to=6139

[37] Results for javascript: URL canonicalization (See https://suika.suikawiki.org/www/url/perl-weburl/t/browsers/decomps.html?decomps-javascript.dat;compat) ( ( 版)) https://suika.suikawiki.org/gate/test-results/list/url-canon-javascript-20110604/all

[38] [whatwg] Javascript: URLs as element attributes ( 版) http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-June/031940.html

[39] [whatwg] URL: javascript URLs ( ( 版)) http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2012-September/037465.html

[40] URL schemes - WHATWG Wiki ( ( 版)) http://wiki.whatwg.org/wiki/URL_schemes

[41] Client-Side JavaScript Reference ( ( 版)) http://docs.oracle.com/cd/E19957-01/816-6408-10/location.htm#1193137

[42] Web Applications 1.0 r5865 Use 'javascript: scheme' instead of 'javascript: protocol'. ( ( 版)) http://html5.org/tools/web-apps-tracker?from=5864&to=5865

[43] Web Applications 1.0 r2529 MAJOR CHANGES: Revamp the way scripts are specified, along with their interaction with resolving relative URLs, etc. Give enough detail to justify objects in the DOM not being garbage collected randomly when still in use. Define script groups, to handle scripts going away during document.open() and session history navigation. Define why and how setTimeout(), database transactions, etc, handle page transitions. Drop the terms 'with' and 'without' script, use script is 'enabled'/'disabled' instead. Define 'unload' and 'beforeunload'. Rework how onfoo= and .onfoo event handler attributes are defined. Rework how the content model of <noscript> is defined. Reword the way javascript: is defined to use the new terminology. Add a few notes of things that came up while I was doing all that.]] ( ( 版)) http://html5.org/tools/web-apps-tracker?from=2528&to=2529

[44] Web Applications 1.0 r2683 Prevent cross-origin javascript: navigation of browsing contexts. Define the base URL and document's address of pages generated by javascript: URLs. Minor editorial tweaks. ( ( 版)) http://html5.org/tools/web-apps-tracker?from=2682&to=2683

[45] URL schemes - WHATWG Wiki ( ( 版)) http://wiki.whatwg.org/wiki/URL_schemes

[46] Bug 13720 – Define javascript: processing entirely inline, and make it only happen in the navigation algorithm; then, remove special-casing elsewhere, and make it non-conforming in those places ( ( 版)) https://www.w3.org/Bugs/Public/show_bug.cgi?id=13720

[70] javascript: URL の処理は HTML Standard で規定されるようになりました。しかし処理方法だけで、意味と構文の規定は行われていません。

[71] WHATWG Wiki のページ (>>45) は、 javascript: URL の意味を規定する、実質的な仕様書となっています。 I-D (>>15) を廃止するものである >>45 と述べられています。

[47] IRC logs: freenode / #whatwg / 20131107 ( ( 版)) http://krijnhoetmer.nl/irc-logs/whatwg/20131107#l-781

[48] Web Applications 1.0 r8285 Define the task settings for the javascript: task ( ( 版)) http://html5.org/tools/web-apps-tracker?from=8284&to=8285

[49] Web Applications 1.0 r8284 Move javascript: processing entirely into HTML, and fix its definitions to match reality better at the same time. ( ( 版)) http://html5.org/tools/web-apps-tracker?from=8283&to=8284

[50] IRC logs: freenode / #whatwg / 20131115 ( ( 版)) http://krijnhoetmer.nl/irc-logs/whatwg/20131115

[51] Web Applications 1.0 r2046 Require that <script src=javascript:...></script> result in no script execution, for any value of '...', for compatibility with most UAs. ( 版) https://html5.org/r/2046

関連

[62] かつて IE 向けにイベントハンドラー内容属性の先頭に javascript: と書くことで JavaScript であると明示する構文がありましたが、 これは URL ではありません。

[64] Part2 - browsersec - Browser Security Handbook, part 2 - Browser Security Handbook - Google Project Hosting ( 版) https://code.google.com/p/browsersec/wiki/Part2#URL_scheme_access_rules

[72] Location Hack does not work · Issue #2232 · greasemonkey/greasemonkey ( 版) https://github.com/greasemonkey/greasemonkey/issues/2232

[73] 1192821 – Regression: 39.0.3: Greasemonkey's "Location hack" no longer works ( 版) https://bugzilla.mozilla.org/show_bug.cgi?id=1192821

[74] Location hack - GreaseSpot Wiki ( 版) http://wiki.greasespot.net/Location_hack

[75] Integrate Fetch into HTML · whatwg/html@7c5555a ( 版) https://github.com/whatwg/html/commit/7c5555a16f2920c02244c10756bb2f1a11e87a22

[76] Rewrite script execution on top of ES · whatwg/html@4891d18 ( 版) https://github.com/whatwg/html/commit/4891d18aaf2df1d40aa61f467a5a10cfc19dd85d

[77] Add <script type="module"> and module resolution/fetching/evaluation · whatwg/html@cd1a9fb ( 版) https://github.com/whatwg/html/commit/cd1a9fb1e83f7d0bc30be8b34ecdaf444a0b19a4

[83] Fix javascript: URL script execution · whatwg/html@6412172 ( 版) https://github.com/whatwg/html/commit/64121722be80ca4e0ea426a085b5fb1a971303cb

[84] Detail how javascript: return values become response bodies · whatwg/html@9997cd9 ( 版) https://github.com/whatwg/html/commit/9997cd93c65a9f4a640a593c02f01c2c58924457

[86] Integrate with the Referrer Policy spec, part 2 of n ( (domenic著, )) https://github.com/whatwg/html/commit/176e74243c649b709b9959b7d08b327290c2f403

[87] Get origin from a browsing context's active document ( (annevk著, )) https://github.com/whatwg/html/commit/d727bf9335f32ab87109658d8e83b0ee3c2866bf

[88] Extract bits out of a javascript URL through serialization ( (annevk著, )) https://github.com/whatwg/html/commit/4113965994bb5d0c650bae13f28883d525a34002

[89] Fix javascript: URL example explanation ( (domenic著, )) https://github.com/whatwg/html/commit/cdf82d5def226004bb90479c284c22e329a4edbd

[90] Navigate: remove "gone async" and define redirect handling (annevk著, ) https://github.com/whatwg/html/commit/8b630f5e4fa2ec8b0999470d09490bffe6e9a1e3

[91] Navigate: minor javascript URL fixup (annevk著, ) https://github.com/whatwg/html/commit/8f34fc491631f5e940d50950567a4cecee104eec

[92] Handle navigation to `javascript:` URLs as inline script. (#142) (@bzbarsky著, ) https://github.com/w3c/webappsec-csp/commit/479bf6c6e891db0bb1cd7f71be764f3aff6a1a33

[93] Apply CSP to javascript URLs in navigate (mikewest著, ) https://github.com/whatwg/html/commit/9daeebfbdd068357257fcb69486d47a893c36f63

[96] Remove javascript URL case from <a>/<area>/Location hash setter (annevk著, ) https://github.com/whatwg/html/commit/deea9fba916cb6e66c7dcfc57442cf25ac0441a1

[97] Remove javascript URL special case from the API (annevk著, ) https://github.com/whatwg/url/commit/4bf85a08da18ef367e093426abef776d59e8fb7b

[98] Editorial: move base URL from "module script" to "script" (domenic著, ) https://github.com/whatwg/html/commit/5458513792ab00d58e6c91ba48faaa611d034a2e

[99] Make integrity="" work on module scripts (domenic著, ) https://github.com/whatwg/html/commit/9275d955dcd604e959cfcc672e0c234b1b8c00db

[100] Clarify navigation behavior for 'script-src'. (mikewest著, ) https://github.com/w3c/webappsec-csp/commit/7a4577c7975c8afdac0c0fc5a5493059b5660742

[101] Do not use percent decode on strings (annevk著, ) https://github.com/whatwg/html/commit/ce8404fa5d8c2c91725c5262fd69d0d45c227ec8

[102] Do not use percent decode on strings by annevk · Pull Request #3111 · whatwg/html () https://github.com/whatwg/html/pull/3111

[103] Return the completion record result from running a script (mattto著, ) https://github.com/whatwg/html/commit/ad3dda51552aeeeea60540c7f1b513fe45b486a5

[108] Return the completion record result from running a script. by mattto · Pull Request #3888 · whatwg/html () https://github.com/whatwg/html/pull/3888

[109] Return empty on "do not run" for running a script (mattto著, ) https://github.com/whatwg/html/commit/b71f885ec4f6e1a074157036d3deb9f1d6a9aa77

[110] Return empty on "do not run" for running a script. by mattto · Pull Request #3907 · whatwg/html () https://github.com/whatwg/html/pull/3907

[111] Remove overridden reload concept (TimothyGu著, ) https://github.com/whatwg/html/commit/6440ccae7340ea41d3eb5bf8ff0b3b27363eda85

[65] Do not use percent decode on strings (annevk著, ) https://github.com/whatwg/html/commit/ce8404fa5d8c2c91725c5262fd69d0d45c227ec8

[112] Added javascript: scheme specifically and fixed a small comment (#370) (andypaicu著, ) https://github.com/w3c/webappsec-csp/commit/0ab98b910f2953161ad4208042694705b0faddde

[113] Added javascript: scheme specifically and fixed a small comment by andypaicu · Pull Request #370 · w3c/webappsec-csp () https://github.com/w3c/webappsec-csp/pull/370

[114] Make CSP inheritance for javascript: more explicit · Issue #368 · w3c/webappsec-csp () https://github.com/w3c/webappsec-csp/issues/368

[115] Specify a navigated-to new document's URL (jyasskin著, ) https://github.com/whatwg/html/commit/f2c7ea37eed6346affa6f8d336eb63173e39ac45

[117] Consider not special casing document's address handling when loading javascript: · Issue #1565 · whatwg/html () https://github.com/whatwg/html/issues/1565

[118] Specify a new document's URL by jyasskin · Pull Request #4205 · whatwg/html () https://github.com/whatwg/html/pull/4205

[119] VRML97, ISO/IEC 14772-1:1997 -- 4 Concepts () https://www.web3d.org/documents/specifications/14772/V2.0/part1/concepts.html#4.5.4

The Script node's url field may also support custom protocols for the various scripting languages. For example, a script url prefixed with javascript: shall contain ECMAScript source, with line terminators allowed in the string. The details of each language protocol are defined in the annex for each language. Browsers are not required to support any specific scripting language. However, browsers shall adhere to the protocol defined in the corresponding annex of ISO/IEC 14772 for any scripting language which is supported.

[66] Editorial: factor out and clean up javascript: URL processing (domenic著, ) https://github.com/whatwg/html/commit/e13762fb626dfd64e7639b79cb61a766d66bd72f

[78] Editorial: factor out and clean up javascript: URL processing by domenic · Pull Request #4396 · whatwg/html () https://github.com/whatwg/html/pull/4396

[79] Fix document URL setting for javascript: URLs (TimothyGu著, ) https://github.com/whatwg/html/commit/06aa2d682b731e5ebc72169c63744e8608418dd8

[82] Fix document URL setting for javascript: URLs by TimothyGu · Pull Request #4698 · whatwg/html () https://github.com/whatwg/html/pull/4698

[125] Googleがonclickにはhref="javascript:void(0)"も付ける理由? | 指向性メモ - 技術, , https://yudai.arielworks.com/memo/2006/06/24/185137

[95] フォーカスとjavascript:void(0)の話・2 | 指向性メモ - 技術, , https://yudai.arielworks.com/memo/2006/06/30/151521

[124] この時代の一部の人々 (Web 1.0 時代に学んだ人々) はまだ HTML文書Webアプリケーションであることに抵抗を持ってて、 JavaScript を使ったものを HTML として相応しくないと判断しがちなところあるよね。 この記事で引用されてるコメントの人々も、なんかよくわからない理屈で嫌がっている。 具体的な実装にも言及しているものの、 「これは避けるべき悪習」 「リンクとして機能させる気がない」 とか根幹には何かの理想像的なものがあって、それと現実を比べてる。 理解しがたい理屈だけど平成10年代前半くらいから見ればそう特異でもない考え方だったんだよね。 (この記事が書かれた頃は Ajax が普通になりつつあって、価値観の相転移が起こっていた。)