views:

107

answers:

3

if in the website http://www.mysite.com there's an external js file added as

<script src="http://www.yoursite.com/new.js"&gt;&lt;/script&gt; 

within the http://www.yoursite.com/new.js js file, there's an ajax call to a script in http://www.yoursite.com/new.js

in such a case will there be the same-origin policy security problem, as it's calling a script in a site from another website?

+1  A: 

Yes, according to answers to a similar question of mine: http://stackoverflow.com/questions/2093732/same-origin-policy-and-scripts-loaded-from-google-a-vulnerable-solution

naivists
+1  A: 

There will be a problem. new.js run in the scope of mysite.com, not yoursite.com.

EDIT: a more detailed explanation would be: when mysite.com is openning a tag, that script runs in the scope of the current page. The source of the script does not matter: it can be inline, local source, or remote source, it is still considered part of mysite.

As you know, scripts in mysite.com cannot access anything on yoursite.com due to the same origin policy. So you cannot do this.

As an advanced option for cross-origin communication look at jsonp. It will require yoursite.com to provide a special handling, but if you have control on both sites then this should not be a problem.

Nir Levy
can you pleas explain...
Anish
added the best explanation i can provide..
Nir Levy
thanks for the explanation bro
Anish
in http://blog.timothyfisher.com/?p=277 there's a sentence -> "The cross-domain limitation means that you can only communicate from the browser back to the domain from which the JavaScript was served." so i got a doubt whether a script on an external javascript file can have access to the site from which the js file is served even though it's running on a different website.
Anish
mkoistinen
A: 

JSONP is precisely what you're looking for: http://en.wikipedia.org/wiki/JSON

The 5,000m overview is that JSONP uses the same mechanism for requesting external scripts as you're using above. The difference is that your server will recognise this and will package up the JSON response as the argument to a callback method. When your site receives this 'script', it executes it thereby returning the data directly into your callback method.

If you are able to use a framework like jQuery, most of the client side would be transparently handled for you. Check it out here: http://api.jquery.com/jQuery.getJSON/

mkoistinen
in blog.timothyfisher.com/?p=277 there's a sentence -> "The cross-domain limitation means that you can only communicate from the browser back to the domain from which the JavaScript was served." so i got a doubt whether a script on an external javascript file can have access to the site from which the js file is served even though it's running on a different website.
Anish
@anish-m. I'm not sure I understand what you've said, but I can assure you (since I've done it loads of times) that with JSONP you CAN effectively transmit and receive data (including arbitrary objects, etc.) to/from a third-party server. This (very) effectively gets around the same-origin policy that XMLHttpRequest is limited by (http://www.w3.org/TR/XMLHttpRequest/).
mkoistinen