简体   繁体   English

是否可以使用 javascript 来修改脚本元素?

[英]Is it possible to use javascript to modify a script element?

Is there a way to use javascript to modify a script element?有没有办法使用 javascript 来修改脚本元素?

Like for example:例如:

HTML: HTML:

<script id="something" src="/js/file.js"></script>

Javascript: Javascript:

var something = document.getElementById("something");
something.src = "/js/anotherfile.js"

Is it possible?可能吗? Because I have a bit of code that works like that and it sort of doesn't work因为我有一些像那样工作的代码,但它有点不起作用

To be specific, here's the code:具体来说,代码如下:

<!DOCTYPE html>
<html>

<head>
  <title>MyohTheGod's Website</title>
  <link rel="icon" type="image/x-icon" href="/supercorn.gif" defer>
  </link>
  <link id="css" href="/css/dark.css" rel="stylesheet" type="text/css">
  </link>
  <script src="/js/particles.js" defer></script>
  <script src="/js/header.js"></script>
  <script src="/js/theme.js"></script>
  <script>window.alert("Welcome to the Home of MyohTheGod. You can play games, check out our web proxies, and more. Also, please do check out the About page. Press OK to continue...");</script>
</head>

<body>
  -snip-
</body>
<script id="foot" src="/js/footer.js"></script>

</html>
<script>
  -snip-
</script>
var css = document.getElementById("css");
var foot = document.getElementById("foot");

function toggleDLmode(m) {
  -snip-
  if (dlmodebool) {
    css.href = "/css/dark.css"
    foot.src="/js/dark-footer.js"
  } else {
    css.href = "/css/index.css"
    foot.src="/js/footer.js"
  }
}

-snip-

It is working, do you inspect it?它工作正常,你检查它吗? It does changed, but maybe you're thinking, "hm why this /js/anotherfile.js is not downloaded?".它确实改变了,但也许你在想,“嗯,为什么这个 /js/anotherfile.js 没有下载?”。 Well because of the script tag is already rendered and already downloaded, so you can't do that.好吧,因为脚本标签已经渲染并且已经下载,所以你不能这样做。 What you can do though add NEW script tag.通过添加新脚本标签可以做什么。

Maybe this will help How to dynamically change the script src?也许这将有助于如何动态更改脚本 src? . . This links would explain more why your code "does not work".此链接将更多地解释您的代码“不起作用”的原因。

There certainly is.肯定有。 You can use document.scripts which returns an collection that you can iterate through like an array.您可以使用 document.scripts 返回一个集合,您可以像数组一样遍历该集合。 You can change the code using the innerHTML property very much like a normal element.您可以像使用普通元素一样使用 innerHTML 属性更改代码。 See herehttps://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection请参阅此处https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection

Edited to add: If you've got a html page with multiple script tags, the document.script collection has each script in the order they appear.编辑添加:如果您有一个带有多个脚本标签的 html 页面,则 document.script 集合按它们出现的顺序排列每个脚本。 The code below will log out the source (src tag) or the actual javascript for each script element.下面的代码将注销每个脚本元素的源代码(src 标记)或实际的 javascript。

You can also 'write' javascript by setting the innerHTML property.您还可以通过设置 innerHTML 属性来“编写”javascript。

IMHO it's a bit of a solution that's looking for a problem but at least it gives you access to the number of scripts you have.恕我直言,这是一个正在寻找问题的解决方案,但至少它可以让您访问您拥有的脚本数量。

 [...document.scripts].forEach(script => { if (script.src.= '') { console:log("Script source." + script;src). } else { console.log(script;innerHTML); } });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM