简体   繁体   English

可以使用JavaScript / jQuery读取脚本标签src URL内容

[英]Is possible to read script tag src URL content using JavaScript/jQuery

Please guide me. 请指导我。 Is possible to read script tag src URL content using JavaScript/jQuery. 可以使用JavaScript / jQuery读取script标签src URL内容。 Please don't suggest JSON/Ajax/iframe options. 请不要建议JSON / Ajax / iframe选项。

I have tried like 我尝试过像

<script src="http://www.test.com" id="test">

document.getElementById('test').src

but it give only src URL. 但它只提供src URL。

My Exact requirements is, I have two application, each in different web server with www.siteA.com and www.siteB.com. 我的确切要求是,我有两个应用程序,每个应用程序都位于具有www.siteA.com和www.siteB.com的不同Web服务器中。

In Server2, I have cross origin and iframe restriction to access the application. 在Server2中,我具有跨源和iframe限制来访问应用程序。 Example: If a request is coming from server1(siteA) to server2(siteB) via Ajax or iframe, it's restricted to access the siteB page content. 示例:如果请求是通过Ajax或iframe从server1(siteA)到server2(siteB)的,则只能访问siteB页面内容。 But If I load siteB in script tag it's loaded, but I'm not able to get the content. 但是,如果我在脚本标签中加载siteB,则它已加载,但是我无法获取内容。

So I mentioned above, don't suggest iframe and Ajax. 所以我在上面提到了,不要建议iframe和Ajax。 Help me to achieve this. 帮助我实现这一目标。

The best way really is AJAX, but it does sound like CORS is an issue for you. 最好的方法实际上是AJAX,但听起来确实像CORS对您来说是个问题。 The only other option I can think of would be to open the page in a new browser window, although I don't know what kind of user experience implications that will have on your application. 我唯一想到的另一种选择是在新的浏览器窗口中打开页面,尽管我不知道会对您的应用程序产生什么样的用户体验影响。

You can open a new browser window by: 您可以通过以下方式打开新的浏览器窗口:

function showContent(){
    var url = document.getElementById("test").src;
    window.open(url, "_blank");
}

Although if you take this route, you shouldn't put the url on a script tag, because the browser will download it once (via the script tag), and then a second time on the window.open. 尽管采用这种方法,但您不应将url放在script标记上,因为浏览器将一次(通过script标记)下载该URL,然后在window.open上第二次下载。

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

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