我阅读了教程 DIY widgets - How to embed your site on another site Dr. Nic 的 XSS Widgets。
我正在寻找一种将参数传递给脚本标记的方法。例如,要进行以下工作:
<script src="http://path/to/widget.js?param_a=1&param_b=3"></script>
有办法吗?
两个有趣的链接:
最佳答案
我为回答一个 super 老问题而道歉,但在花了一个小时与上述解决方案搏斗之后,我选择了更简单的东西。
<script src=".." one="1" two="2"></script>
在上面的脚本中:
document.currentScript.getAttribute('one'); // 1
document.currentScript.getAttribute('two'); // 2
比 jQuery 或 URL 解析容易得多。
您可能需要来自 @Yared Rodriguez's answer 的 document.currentScript
的 polyfill对于 IE:
document.currentScript = document.currentScript || (function() {
var scripts = document.getElementsByTagName('script');
return scripts[scripts.length - 1];
})();
关于javascript - 如何将参数传递给脚本标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5292372/