我如何引用加载当前正在执行的脚本的脚本标签? [英] How may I reference the script tag that loaded the currently-executing script?

查看:32
本文介绍了我如何引用加载当前正在执行的脚本的脚本标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何引用加载了当前正在运行的 javascript 的脚本元素?

How can I reference the script element that loaded the javascript that is currently running?

情况是这样的.我有一个主"脚本被加载到页面的高处,第一件事是在 HEAD 标签下.

Here's the situation. I have a "master" script being loaded high in the page, first thing under the HEAD tag.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type="text/javascript" src="scripts.js"></script>

scripts.js"中有一个脚本需要能够按需加载其他脚本.普通方法对我来说不太适用,因为我需要在不引用 HEAD 标签的情况下添加新脚本,因为 HEAD 元素还没有完成渲染:

There is a script in "scripts.js" which needs to be able to do on-demand loading of other scripts. The normal method doesn't quite work for me because I need to add new scripts without referencing the HEAD tag, because the HEAD element hasn't finished rendering:

document.getElementsByTagName('head')[0].appendChild(v);

我想要做的是引用加载当前脚本的脚本元素,以便我可以将新的动态加载的脚本标签附加到它之后的 DOM 中.

What I want to do is reference the script element that loaded the current script so that I can then append my new dynamically loaded script tags into the DOM after it.

<script type="text/javascript" src="scripts.js"></script>
loaded by scripts.js--><script type="text/javascript" src="new_script1.js"></script>
loaded by scripts.js --><script type="text/javascript" src="new_script2.js"></script>

推荐答案

如何获取当前脚本元素:

1.使用 document.currentScript

document.currentScript 将返回当前正在处理其脚本的

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆