if document.currentScript Return null, which usually means that the JavaScript file is not passed <script> Tags are imported directly, but loaded through other methods, such as using a module loader (such as RequireJS) or dynamically created through JavaScript <script> element.
In this case, you can try the following workarounds:
Pass variables directly: If you use another way to load the JavaScript file, you can pass variables to functions or objects in the JavaScript file directly from where the file is loaded. For example, if you use a module loader, you can pass variables as arguments to functions in the module.
Wait for the DOMContentLoaded event to fire before executing JavaScript code:
DOMContentLoadedAfter the event is triggered, it is guaranteed that the entire document has been parsed, including the loading of the JavaScript file. you canDOMContentLoadedExecute JavaScript code within the event handler rather than relying ondocument.currentScript。
for instance:
document.addEventListener('DOMContentLoaded', function() {
var player = videojs('my-video');
// 获取 videoMd5 值的方式可能需要调整,具体根据你的情况来决定
var videoMd5 = '<?= $videoMd5 ?>';
var progress = localStorage.getItem('video_progress_' + videoMd5);
if (progress) {
player.currentTime(progress);
}
player.on('timeupdate', function() {
localStorage.setItem('video_progress_' + videoMd5, player.currentTime());
});
});Make sure to DOMContentLoaded Executing JavaScript code within an event listener can resolve the issue in some cases document.currentScript Return null problem.