简体   繁体   English

如何从脚本标记中读取JS作为数据?

[英]How do I read JS as data from a script tag?

I have a external file (let's say foo.js) 我有一个外部文件(让我们说foo.js)

function baz() {} 

Then in my HTML, I import it with the script tag: 然后在我的HTML中,我使用脚本标记导入它:

<script type="text/javascript" src="foo.js"></script>

I want to be able to get a string of the JS from inside of the script tag. 我希望能够从脚本标记内部获取JS的字符串。 I have tried jquery's html(), and the innerHTML and innerText attributes, but they all return empty strings. 我尝试过jquery的html(),以及innerHTML和innerText属性,但它们都返回空字符串。

Note: I am trying to avoid AJAX, because my server is slow, and to decrease the size of the webpage, even with caches. 注意:我正在尝试避免使用AJAX,因为我的服务器速度很慢,并且即使使用缓存也会减小网页的大小。

Edit: The string I want to get would contain the data in the javascript file, not its URL: 编辑:我想要获取的字符串将包含javascript文件中的数据,而不是其URL:

getJsData(document.querySelector('script[src="foo.js"]')) == 'function baz() {}'

I may not exactly understand what is it and why you want to implement this. 我可能不完全理解它是什么以及为什么要实现它。 Considering that you do not want to use ajax due to slow server issues , you might as well do it old school. 考虑到由于服务器问题导致您不想使用ajax ,您可以选择旧学校。 If your page is not very heavy, you can - 如果您的页面不是很重,您可以 -

  • Put a hidden iframe on the page pointing its src to your JS file. 在页面上放置一个隐藏的 iframe ,将其src指向您的JS文件。

  • Wait for the $('document').ready() to be called inside the iframe, ie let the iFrame load all the content. 等待在iframe中调用$('document').ready() ,即让iFrame加载所有内容。

  • Copy the contents of the iframe one its loaded into the HTML element container you want. 将其加载的iframe的内容复制到所需的HTML元素容器中。

Hope this helps! 希望这可以帮助!

在脚本标记中放入一个id,并从Jquery选择器中获取该元素

<script type="text/javascript" id="my_id" src="foo.js"> </script>

<script> var my_js_file = $("#my_id").attr("src"); my_js_file.trim(".js"); </script>

Incidentally, this can be done without the need for iframes or ajax . 顺便说一句,这可以在不需要iframesajax

Since your external function is available to you; 由于您可以使用外部功能; simply just invoke toString() on it. 只需在其上调用toString() eg: 例如:

window.console.log(baz.toString());

Here is an example that demonstrates a typical valid reason for wanting to do this kind of thing & also it's limitations: 这是一个示例,演示了想要做这种事情的典型正当理由以及它的局限性:

 var strMod=GameOfLife.toString(); //real shenzi prettification lol (strMod.indexOf(";var")>-1) && (strMod=strMod.replace(/;/g, ";\\t\\n")); var nodeCtr=document.getElementById('code-block'); var nodeCodes=document.createElement("pre"); nodeCodes.classList.add("code-snippet"); nodeCtr.appendChild(nodeCodes); nodeCodes.innerHTML="var GameOfLife="+strMod; SyntaxHighlighter.highlight({brush: "js"}, nodeCodes); 
 body { background-color:#131313; } a:link, a:visited { color: rgba(185, 176, 218, 0.78); text-decoration: none; } a:hover, a:focus { color: rgba(41, 129, 241, 0.78); cursor: pointer; cursor: hand; text-decoration: underline; } #code-block { max-width:800px; overflow:auto; } #code-block .syntaxhighlighter { margin:20px; font-size:10px!important; overflow:visible!important; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/scripts/shCore.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/scripts/shBrushJScript.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/styles/shCore.min.css" rel="stylesheet"/> <link href="https://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/styles/shThemeEmacs.css" rel="stylesheet"/> <script src="https://cdn.rawgit.com/nomatteus/conway-game-of-life-js/master/gameoflife.js"></script> <a href="https://github.com/nomatteus/conway-game-of-life-js"> Conways Game of Life ~ js-stylee! ~ by Matthew Ruten </a> <div id="code-block"></div> 

&fiddle with it here 在这里摆弄它


As can be seen; 可以看出; in some browsers, this particular usage is going to work more or less well, depending upon the toString() implementation. 在某些浏览器中,这种特殊用法或多或少会有效,具体取决于toString()实现。 -all of which is discussed in this nice little article - 在这篇不错的小文章中讨论了所有这些内容

Another caveat: this trick also won't work so well on Objects that are not functions or functions that immediately execute (ie: RMP ) etc. 另一个警告:这个技巧也不会在不是立即执行的函数或函数的Objects上工作得很好(即: RMP )等。

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

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