469,631 Members | 1,476 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Iframe content change detection

Hi guys
We have a requirement like in one iframe we have to show one site and in another iframe we have to show some other external site, we did it using

[HTML]
<iframe src="http://www.a.com"> </iframe>
<iframe src="http://www.b.com"> </iframe>
[/HTML]

now if the session expires in any one site, the login page is shown in only the correponding iframe, what we require is that the login page has to be shown in the entire window(since both have the same credentials) instead of an Iframe,

We thought of a javascript which should run after each content change of iframe
and we will be checking the content present in the corresponding iframe for loginpage contents, if it contains the loginpage we will be setting the window location to login page.

What we need is the way to run a javascript after the content got changed in a iframe(External site contents).

Your Help is highly appreciated
Dec 6 '07 #1
9 16004
acoder
16,027 Expert Mod 8TB
If the iframe and the main page are from different domains, this will not be possible with JavaScript.
Dec 6 '07 #2
jad2006, did you found a solution? I can think of some other ways using AJAX ..

Dan
Dec 6 '07 #3
Hi Dan/Acoder

I found a solution using javascript.Following is the code
[HTML]


<iframe src="http://www.a.com"> </iframe>
<iframe src="http://www.b.com" name="frame2" onLoad="javascript:checkForLoginPage()"> </iframe>

[/HTML]

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function checkForLoginPage(){
  3. var ad=frame2.document;
  4.  
  5. if(ad!=null)
  6. var bodyText= ad.getElementsByTagName("body").item(0).innerHTML;
  7. //as my loginpage contains this text
  8. if(bodyText.indexOf('Forgot your Password?')>0){
  9. //clear the cookies
  10. //set window.location to login page;
  11.  
  12. window.location.href=loginurl;
  13. }
  14. }
  15. </script>         
  16.  
  17.  
Dan,
Please Share the Solution u r thinking of..

Thanks & Regards
Jad
Dec 7 '07 #4
Dasty
101 Expert 100+
Hi Dan/Acoder

I found a solution using javascript.Following is the code
[HTML]


<iframe src="http://www.a.com"> </iframe>
<iframe src="http://www.b.com" name="frame2" onLoad="javascript:checkForLoginPage()"> </iframe>

[/HTML]

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function checkForLoginPage(){
  3. var ad=frame2.document;
  4.  
  5. if(ad!=null)
  6. var bodyText= ad.getElementsByTagName("body").item(0).innerHTML;
  7. //as my loginpage contains this text
  8. if(bodyText.indexOf('Forgot your Password?')>0){
  9. //clear the cookies
  10. //set window.location to login page;
  11.  
  12. window.location.href=loginurl;
  13. }
  14. }
  15. </script>         
  16.  
  17.  
Dan,
Please Share the Solution u r thinking of..

Thanks & Regards
Jad
This is not gonna work. You just can not access document's properties/mothods when iframe got external (from different domain) page loaded (as you said). acoder is right
Dec 7 '07 #5
Hello jad2006,
I thought using that method also, but what I get on both Firefox and IE(the browsers that I usually test on) is the following exception:
"Permission denied to get property HTMLDocument.getElementsByTagName" when trying to get content from iFrame.
Am I missing something?

Dan
Dec 7 '07 #6
I see Dasty already replied to that. Other methods that I can think of (using AJAX) require some extra coding on both websites that you display(I consider this important as a measure of security on a website - client side scripting can create vulnerabilities on your website).

jad2006, can you tell more about the content you are showing in this pages?

Dan
Dec 7 '07 #7
acoder
16,027 Expert Mod 8TB
Hi Dan/Acoder

I found a solution using javascript.Following is the code
[HTML]


<iframe src="http://www.a.com"> </iframe>
<iframe src="http://www.b.com" name="frame2" onLoad="javascript:checkForLoginPage()"> </iframe>

[/HTML]<snipped>
The only way this would work is if the main page is from domain b.com.
Dec 10 '07 #8
Sorry guys
I simply tested it by using giving some static pages in Iframe1 and iframe2 , I have not tested in the real scenario, I am wrong,Thanks everyone for ur valuable suggestios, but is there any way out to do this?
Dec 12 '07 #9
acoder
16,027 Expert Mod 8TB
You could use a web proxy via Ajax. Use server-side code to get the contents, then return the response to the client-side.
Dec 13 '07 #10

Post your reply

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

Similar topics

11 posts views Thread by HolaGoogle | last post: by
9 posts views Thread by cakewalkr7 | last post: by
3 posts views Thread by PCgeek | last post: by
1 post views Thread by CJ4R | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.