能的JavaScript访问源$ C ​​$ A&LT了C;脚本SRC ="">元件? [英] Can JavaScript access source code of a <script src=""> element?

查看:107
本文介绍了能的JavaScript访问源$ C ​​$ A&LT了C;脚本SRC ="">元件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<script id="s1" src="foo.js"></script>
<script>
    alert('foo.js contains' + _source_code_of('s1'))
</script>

能否 _source_ code_of 实施?

推荐答案

没有,这将允许检索任何URL,这将打破一些安全策略的内容。 (这将是不相同的域检查一个Ajax GET请求的等效。)

No, this would allow to retrieve the contents of any URL, which would break some security policies. (This would be an equivalent of an ajax get request without same-domain checks.)

不过,由于 foo.js 是同一个域比页面,您可以使用一个Ajax请求获取它。示例使用j​​Query:

However, since foo.js is on the same domain than the page you can fetch it with an ajax request. Example with jQuery:

$.get('foo.js', function(source_code) {
    alert('foo.js contains ' + source_code);
});

这篇关于能的JavaScript访问源$ C ​​$ A&LT了C;脚本SRC =&QUOT;&QUOT;&GT;元件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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