javascript 获取 <script> 内容作为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5364554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:56:59  来源:igfitidea点击:

get <script> content as String

javascriptjqueryhtml

提问by orshachar

I tried using Javascript on demand to override cross domain problems.

我尝试按需使用 Javascript 来覆盖跨域问题。

So now I have something like that on my page:

所以现在我的页面上有类似的内容:

<script id="scr1" src="{some other domain url}"></script>

The content of this script is a simply an array:

这个脚本的内容只是一个数组:

["hello", "world", "what", "where"]

I want to somehow get this content as string so I can eval it.
I wish I had some JS/JQuery method like

我想以某种方式将此内容作为字符串获取,以便我可以对其进行评估。
我希望我有一些 JS/JQuery 方法,比如

var v = eval($("#scr1").getContent());

Can you help me please?

你能帮我吗?

Note: I can't use ajax as a solution.

注意:我不能使用 ajax 作为解决方案。

回答by tvanfosson

You should look up JSONP. What you want your script to return is the invocation of a callback with the data as the argument.

您应该查找 JSONP。您希望脚本返回的是使用数据作为参数调用回调。

<script id="scr1" src="http://www.example.com/some/action?callback=cb" type="text/javascript></script>

The server side then, would produce the script contents:

然后,服务器端将生成脚本内容:

cb( ["hello", "world", "what", "where"] );

Then on in your page, you'd need to have a function named cbthat expects an array.

然后在您的页面中,您需要有一个需要cb数组的名为的函数。

function cb(data) {
  // do something with the array
}

Of course, this expects the cooperation of the server to understand your callback parameter and respond appropriately. If you control the server, that's pretty easy to do. If not, then you'll need to use an API that is constructed to return JSONP. The callbackparameter is a pretty standard name for such frameworks. If they don't have an API, then you need to proxy the request on your server, in which case you can use standard JSON.

当然,这需要服务端的配合来理解你的回调参数并做出适当的响应。如果你控制服务器,这很容易做到。如果没有,那么您将需要使用构造为返回 JSONP 的 API。该callback参数是此类框架的一个非常标准的名称。如果他们没有 API,那么您需要在您的服务器上代理请求,在这种情况下,您可以使用标准 JSON。

FWIW, jQuery makes this pretty easy for APIs that expect to return JSONP.

FWIW,对于期望返回 JSONP 的 API,jQuery 使这变得非常容易。

$.getJSON( 'http://example.com/api/method?callback=?', data, function(result) {
     // do something with the json result
});

You can also use this with post/get/ajax methods as well, but getJSONis easier. Note that including the callback parameter automatically tells it to use a JSONP (script) request and automatically fills in the name of the callback it creates to wrap the (anonymous) callback function in the request.

您也可以将它与 post/get/ajax 方法一起使用,但getJSON更容易。请注意,包含回调参数会自动告诉它使用 JSONP(脚本)请求并自动填写它创建的回调的名称,以将(匿名)回调函数包装在请求中。

回答by Paul Creasey

should be possible with jquery.getScript

应该可以使用 jquery.getScript

$.getScript('urlOfRemoveScript',function(script){
    var v = eval(script);
});

Not 100% sure this will work though.

不是 100% 确定这会起作用。

回答by Miere

So here is my contribution if someone really needs to find a way to read every script content.

所以如果有人真的需要找到一种方法来阅读每个脚本内容,这是我的贡献。


  jQuery("script").each(function(){
    var scriptContent = this.innerText
    // do something with scriptContent
  })