469,330 Members | 1,306 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,330 developers. It's quick & easy.

Cross Domain Scripting

dmjpro
2,476 2GB
Is it possible to do Cross Domain Scripting in JavaScript.
Basically i need to access some elements of an application from another application ..Is it possible to do using JavaScript?
Please Help!
Dec 18 '08 #1
28 1961
acoder
16,027 Expert Mod 8TB
It is. You can use dynamic script tags which allow code form remote sites, e.g. JSON, to be included.

You can also use a cross-domain proxy, but that depends on server-side code.

For sub-domains, use the document.domain property.
Dec 18 '08 #2
dmjpro
2,476 2GB
@acoder
Can you give some related examples or any link where i can teach myself ..
Thanks!
Dec 18 '08 #3
acoder
16,027 Expert Mod 8TB
For what? All of the techniques or one particular one?
Dec 18 '08 #4
dmjpro
2,476 2GB
It would be better if you could give me all of the techniques ...
Thanks!
Dec 18 '08 #5
dmjpro
2,476 2GB
What I need experts that ..... I have an application which contains some frames.
Inside a frame there is a page from where i am trying to access some frames which is in another domain, placed in a frame of the previous application.
Just i have to put a button by clicking i want to hide all of those frames...
Now some frames are in one application and some in another application.
So how do i access those frame or frameset properties from that application...?
Please Help..Thanks!
Dec 18 '08 #7
dmjpro
2,476 2GB
@acoder
Thanks for your Help Acoder.
But here i am not using any Ajax Call.
And first two links are related to Ajax ...
Dec 18 '08 #8
dmjpro
2,476 2GB
Expand|Select|Wrap|Line Numbers
  1. function getFullScreen(){
  2.       var top_frameset = window.parent.document.getElementById('top_frameset');
  3.       var bottom_frameset = window.parent.document.getElementById('bottom_frameset');
  4.  
  5.       window.parent.top_frameset_rows = top_frameset.rows;
  6.       window.parent.bottom_frameset_cols = bottom_frameset.cols;
  7.  
  8.       var main_page = window.parent.frames['top_welcome'];
  9.       var main_page_framesets = main_page.document.getElementsByTagName('frameset');
  10.  
  11.       top_frameset.rows = "8%,*";
  12.       bottom_frameset.cols = "100%,*";
  13.       window.parent.frames['top_include'].location.href = "./temp_top_include.jsp";
  14.       if(main_page_framesets.length==0){
  15.  
  16.       }else if(main_page_framesets.length==1){
  17.         window.parent.main_page_cols = main_page_framesets[0].cols;
  18.         main_page_framesets[0].cols = "0%,*";
  19.       }else if(main_page_framesets.length==2){
  20.         window.parent.main_page_rows = main_page_framesets[0].rows;
  21.         window.parent.main_page_cols = main_page_framesets[1].cols;
  22.         main_page_framesets[0].rows = "0%,*";
  23.         main_page_framesets[1].cols = "0%,*";
  24.       }
  25.   }
  26.  
This is my JavaScript code...

Expand|Select|Wrap|Line Numbers
  1. var main_page_framesets = main_page.document.getElementsByTagName('frameset');
  2.  
This line gives me the error ..because top_welcome frame now contains the cross domain.
I think you get my point .... Please Help..Thanks!
Dec 18 '08 #9
rnd me
427 Expert 256MB
with fiefox and i think webkit nightlies, you can send cross-window messages.

more on that

for small-scale stuff, you can read jsnop from anywhere. if you can write json with ajax in one domain, it can be read from another.

you can also pass data cross domain using the query string (location.search), but this is more of a one-time thing, not useful for communicating actions, just data.
Dec 18 '08 #10
dmjpro
2,476 2GB
Is it for Firefox only, then what's for ...IE?
Dec 18 '08 #11
acoder
16,027 Expert Mod 8TB
IE doesn't support it as yet. Although the proxy link describes how to do it with Ajax, you can always adapt it for your requirements. Do you have control over the other domain?
Dec 18 '08 #12
dmjpro
2,476 2GB
Yeah i have the control ... but Acoder ....for our application we are bypassing the Proxy. So can't we do this without using Proxy?
Dec 18 '08 #13
acoder
16,027 Expert Mod 8TB
By proxy, I meant serving the page from the first domain, e.g. with PHP you could use Curl. Have a read of this to get an idea. However, since you have control of both domains, you can use something like this.
Dec 18 '08 #14
dmjpro
2,476 2GB
I could not figure out anything ...please could you show me some sample code ... :)
Dec 30 '08 #15
acoder
16,027 Expert Mod 8TB
There's a demo right there.
Dec 30 '08 #16
dmjpro
2,476 2GB
The link is not working here ............... :)
Dec 30 '08 #17
dmjpro
2,476 2GB
I had a look at this link ...
But i still can't find out to access the cross domain elements what should i do ? :(
Dec 30 '08 #18
dmjpro
2,476 2GB
In my situation what should i be doing ...i am getting messed up .... :)
Dec 30 '08 #19
acoder
16,027 Expert Mod 8TB
Not sure what happened with the link, but I see you did find it.

All you have to do is study the code which is linked to from the top (or you can always look at the source).
Dec 30 '08 #20
dmjpro
2,476 2GB
In this link a iframe is there ... which refers to cross domain
Please check out it's source code ...
There no buttons has onClick handler then how it fires a code on Click event ?
Now how it finds out the function body of $ as it's defined in another JS file?

Please clarify me these queries.. :)
Dec 30 '08 #21
gits
5,390 Expert Mod 4TB
the script is included in the page ... just have a look here ... there it is ;)

kind regards
Dec 30 '08 #22
dmjpro
2,476 2GB
Can you help me to do .cross domain scripting ...Did you see my code snippet ?
Dec 30 '08 #23
acoder
16,027 Expert Mod 8TB
Which code snippet? I would suggest you take the source code of the example which works and adapt it to your requirements. Of course, it also helps if you read the article to understand how it all works.
Dec 30 '08 #24
dmjpro
2,476 2GB
If i had no control over the second application then how it would possible to access the elements of second application from first application?
Please have a look at the 9th thread .... ;)
Dec 31 '08 #25
acoder
16,027 Expert Mod 8TB
You said earlier that you did have control (see post #13). If not, one option is the one I suggested earlier (cross-domain proxy). This isn't limited to Ajax. The idea is quite simple and explained in the earlier links.
Dec 31 '08 #26
dmjpro
2,476 2GB
Today morning i tested out a code which allows me to access the cross domain elements.
Have a look at my code .........
This is the home page of Domain1.
Expand|Select|Wrap|Line Numbers
  1. <frameset border="0" rows="50%,*">
  2.             <frame name=domain1 id=domain1 src="index.jsp?"/>
  3.             <frame name=domain2 id=domain2 src="http://localhost:8084/Domain2/"/>
  4.             </frameset>
  5.  

The content of index.jsp which is from Domain1....
Expand|Select|Wrap|Line Numbers
  1. <head>
  2. <script type='text/javascript'>
  3.             function callMe(){
  4.  window.parent.frames['domain2'].document.getElementById('text_box').value = 'Debasis Jana';
  5.             }
  6.         </script>
  7.     </head>
  8.     <body>
  9.         <input type=button name=click value="Click Me" onclick="callMe()"/>
  10. </body>
  11.  
Now the home page of Domain2 ......
Expand|Select|Wrap|Line Numbers
  1. <body>
  2.         <input type=text name=text_box id=text_box value=""/>
  3.     </body>
  4.  
Now how it is possible to access the text box[of Domain2] from Domain1?
Can anyone help me to figure me out ?
Jan 29 '09 #27
acoder
16,027 Expert Mod 8TB
In addition to the link posted by gits in post #22, take this and adapt it. I suggest the best course of action is to take the two pages from the different domains and try getting it to work at your domains. Once you have it working, it should be trivial to adapt it to your requirements.
Jan 29 '09 #28
dmjpro
2,476 2GB
Anyway now i understood what is cross domain ...
One more good news .... Thanks Acoder ..now i able to manage my things work . The link is very useful to me ... ;)

Actually i could not get the location hash but i could set the hash value ..really it was amazing to work with that link .. thanks Acoder.
Jan 29 '09 #29
acoder
16,027 Expert Mod 8TB
No problem at all. Glad to help.
Jan 29 '09 #30

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Charles Crume | last post: by
7 posts views Thread by CJD | last post: by
25 posts views Thread by VA | last post: by
4 posts views Thread by Jacob JKW | last post: by
1 post views Thread by torsten.reiners | last post: by
dmjpro
1 post views Thread by dmjpro | last post: by
3 posts views Thread by rengaraj | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.