繁体   English   中英

Javascript-从脚本内部获取脚本自己的源代码

[英]Javascript - Getting a script's own souce code from within the script

因此,我有一个问题,我需要从脚本本身内部以字符串形式获取脚本的整个源代码(无论是内联脚本还是使用src属性的脚本),但事先不确切知道它是什么脚本。 由于这个原因,我不能只是做一个XHR。 页面上的几个脚本将侦听事件,并且在处理事件时,它们还将运行标识脚本自身源代码的代码。 我应该怎么做?

function identifySelf() {
    // Some code here
    return mysource; // should return the source code of the script in which this function is defined
}

您将需要标识每个脚本标签和一个ID。 然后

var script=document.getElementById("my_id");
var content=script.innerHTML || "";
if(content==null || content==""){
    var src=script.src;
    // ajax in the script, I'm not going to write the code because I'm on mobile
    // I'm assuming you can write it yourself
}

编辑:对不起,我没有完全阅读您的问题。 如果需要立即返回内容,则需要在页面加载时对脚本的内容进行ajax,并返回缓存的内容。 没有办法像这样获得innerHTML。

暂无
暂无

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

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