如何在变量中收集HTML页面的所有脚本标记 [英] How to collect all script tags of HTML page in a variable

查看:121
本文介绍了如何在变量中收集HTML页面的所有脚本标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想收集所有< script> ....< / script> HTML页面中的某些变量中存在的代码部分。

I would like to collect all the <script> ....</script> code section present in the HTML page in some variable.

这样做的简单方法应该是什么,不知道如何使用JavaScript检索它。??

What should be the simpler way to do this, Any idea how it can be retrieved using JavaScript.??

任何帮助将不胜感激。

推荐答案

获取可以使用的脚本列表

To get a list of scripts you can use


  • document.getElementsByTagName(script); by tag

  • document.scripts; 内置集合

  • document.querySelectorAll(script); by selector

  • $(script) jQuery by selector

  • document.getElementsByTagName("script"); by tag
  • document.scripts; Built-in collection
  • document.querySelectorAll("script"); by selector
  • $("script") jQuery by selector

var scripts = document.getElementsByTagName("script");
for (var i = 0; i < scripts.length; i++) {
  if (scripts[i].src) console.log(i, scripts[i].src)
  else console.log(i, scripts[i].innerHTML)
}

// To get the content of the external script 
// - I use jQuery here - only works if CORS is allowing it

// find the first script from google 
var url = $("script[src*='googleapis']")[0].src; 

$.get(url,function(data) { // get the source 
  console.log(data.split("|")[0]); // show version info
});  

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
  console.log("Inline script");
</script>
<script>
  function bla() {
    console.log("Other inline script");
  }
</script>

这篇关于如何在变量中收集HTML页面的所有脚本标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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