html5 script for,GitHub - JamesMGreene/document.currentScript: Polyfill for HTML5's `document.curren...

本文介绍了一个HTML5的polyfill方案,用于在IE6-10中实现document.currentScript的功能。该方案遵循HTML规范定义的行为,并在特定条件下返回当前执行脚本元素。此外还提供了一个替代方案来获取当前执行的脚本,不论其触发方式。

document.currentScript

e43e2c3d66eceecc88581b96aae995b5.png722ddbe7173a82b060a06505858d81a8.png68747470733a2f2f73617563656c6162732e636f6d2f6275696c647374617475732f4a616d65734d477265656e655f646373a987a8103b8f7cfec9d52e44ff35df68.pngc4060278b846a7a4bedb13cb4e2cd52b.png

68747470733a2f2f73617563656c6162732e636f6d2f62726f777365722d6d61747269782f4a616d65734d477265656e655f6463732e737667

A polyfill of HTML5's document.currentScript for IE 6-10 ONLY.

Public Service Announcement (PSA)

This polyfill will not work in IE11 because of a critical design choice made Microsoft ("Don't Call Me IE!") [1][2][3][4] in order to avoid consumers receiving an unnecessarily downgraded experience on websites that were making logic branch and feature decisions based on browser detection rather than feature detection.

However, Microsoft Edge (a.k.a. "Spartan", a.k.a. IE12) does natively support document.currentScript. This is likely due in part to you lovely consumers upvoting this issue on the IE Platform Suggestion Forum, so thank you!

Overview

Strict

This polyfill is configured do its best to comply with the HTML spec's definition of the correct behavior for document.currentScript.

More particularly, this will get the script element that was the source of the nearest currently executing code but ONLY if said source script is being evaluated synchronously for the first time by the browser.

In other words, if code is being operated on after its initial evaluation (e.g. async callbacks, functions invoked by user actions, etc.), then document.currentScript will always return null.

Loose

If you are interested in getting the currently executing script [regardless of the source/trigger of the execution], take a look at JamesMGreene/currentExecutingScript instead.

Browser Compatibility

Browser

Version

Works?

Notes

IE

6

Yes

Must use document._currentScript().

IE

7

Yes

Must use document._currentScript().

IE

8

Yes

IE

9

Yes

IE

10

Yes

IE

11

NO!

IE removed script.readyState but didn't add document.currentScript yet! 😲 See PSA for more info.

Installation

NPM

npm install currentscript

GitHub

Alternatively, you can download/clone its GitHub repo: JamesMGreene/document.currentScript

Usage

IE 8-10

var scriptEl = document.currentScript;

IE 6-7

var scriptEl = document._currentScript();

Configuration

doNotDeferToNativeMethod

To support better cross-browser support, the default behavior of this polyfill in browsers other than IE 6-10 is to attempt to retrieve the native document.currentScript accessor method and use it as a last-ditch fallback effort.

However, if you would prefer to disallow that fallback behavior, you can do so as follows:

document._currentScript.doNotDeferToNativeMethod = true;

Other Documentation

Errata

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值