1

Please guide me. Is possible to read script tag src URL content using JavaScript/jQuery. Please don't suggest JSON/Ajax/iframe options.

I have tried like

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

document.getElementById('test').src

but it give only src URL.

My Exact requirements is, I have two application, each in different web server with www.siteA.com and www.siteB.com.

In Server2, I have cross origin and iframe restriction to access the application. 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. But If I load siteB in script tag it's loaded, but I'm not able to get the content.

So I mentioned above, don't suggest iframe and Ajax. Help me to achieve this.

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
AbnSrn
  • 566
  • 2
  • 6
  • 20
  • 1
    Yes! It is very much possible. Have you tried anything which did not work ? – Rayon Nov 19 '15 at 14:06
  • 1
    Sharing your research helps everyone. Tell us what you've tried and why it didn't meet your needs. This demonstrates that you've taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! Also see [how to ask](http://stackoverflow.com/questions/how-to-ask) – Cerbrus Nov 19 '15 at 14:10
  • @rayon and cerbrus: I have updated my question – AbnSrn Nov 19 '15 at 14:11
  • 1
    @Anbazhagan: You mean you want the content of the *target* URL, not the URL itself? In that case it sounds like an AJAX request would be the way to go. – David Nov 19 '15 at 14:11
  • 1
    Duplicate of: [How can I get the content of the file specified as the 'src' of a – Cerbrus Nov 19 '15 at 14:12
  • 4
    You can only do it with AJAX, and then only if CORS doesn't prevent it. – Alnitak Nov 19 '15 at 14:21

1 Answers1

0

The best way really is AJAX, but it does sound like CORS is an issue for you. 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.

M Falanga
  • 1,897
  • 17
  • 21
  • If I open in new window i cant able to read the content of that page and also need siteB control in siteA. – AbnSrn Nov 20 '15 at 15:13