English 中文(简体)
页: 1
原标题:get <script> content as String

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

因此,我现在就这样说:

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

该文字的内容只是一个阵列:

["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

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

您能否帮助我?

注:我可以把麻.作为解决办法。

最佳回答

你们应当看一看初等人物。 你们想要交回的文字就是援引数据作为理由。

<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"] );

然后,在您的网页上,你需要一个名为<条码>cb的功能,希望能有一个阵列。

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

当然,这期望服务器的合作能够理解你的备用参数并作出适当的反应。 如果你控制服务器,这非常容易。 否则,你就不得不使用为遣返JSONP而建造的APIC。 <编码>查询<>代码>参数是此类框架的相对标准名称。 如果他们没有APICA,那么你需要把请求引向服务器,在这种情况下,你可以使用标准的JSON。

FWIW, jQuery, 这使预期要返回的APICES非常容易。

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

也可以使用邮政/植被/动物方法,但getJSON更容易。 请注意,包括备用参数自动告诉它使用JSONP(书写)申请,并自动填充请求中(匿名)报警功能。

问题回答

should be possible with jquery.getScript

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

这肯定不会达到100%。

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
  })




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签