admin管理员组文章数量:1404612
Is there any way to get the URLs of external CSS & Javascripts loaded in the head of a document?
so for instance in head:
<script type="text/javascript" src="/path/somewhere/script.js">
<link rel="stylesheet" href="/path/somewhere/styles.css">
How would I get /path/somewhere/script.js
and /path/somewhere/styles.css
from DOM?
Is there any way to get the URLs of external CSS & Javascripts loaded in the head of a document?
so for instance in head:
<script type="text/javascript" src="/path/somewhere/script.js">
<link rel="stylesheet" href="/path/somewhere/styles.css">
How would I get /path/somewhere/script.js
and /path/somewhere/styles.css
from DOM?
2 Answers
Reset to default 5Just use querySelector
and extract the appropriate properties:
const script = document.querySelector('head > script');
console.log(script.src);
const css = document.querySelector('head > link');
console.log(css.href);
(doesn't work in snippet due to sandboxing)
Or if you need all script/css URLs, and not just one of each, loop over them:
document.querySelectorAll('head > script')
.forEach(script => console.log(script.src));
document.querySelectorAll('head > link')
.forEach(css => console.log(css.href));
<script>
let scriptTags = document.querySelectorAll('head > script');
scriptTags.forEach(scriptTag => {
console.log(scriptTag.getAttribute('src'));
});
let linkTags = document.querySelectorAll('head > link');
linkTags.forEach(linkTag => {
console.log(linkTag.getAttribute('href'));
});
</script>
本文标签: javascriptGet url of scripts and styles in document headStack Overflow
版权声明:本文标题:javascript - Get url of scripts and styles in document head - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744834874a2627569.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论