Solved

How do I detect what page iframe is loaded in and change iframe src if not loaded in intended page?

Posted on 2009-05-18
4
609 Views
Last Modified: 2012-05-07
Example:

MyFramePage.aspx gets loaded in an iFrame in MainPage.aspx  MyFramePage.aspx should check to see if it is loaded in MainPage.aspx if it is not MyFramePage should redirect/reload another page in its place.  

The url for MainPage.aspx will also have a query string appended like MyMainPage.aspx?id=xxxxxx so the script in MyFramePage.aspx should contain something like if parent contains MyMainPage.aspx and ignore the query part.  I need the Javascript to accomplish this.  

 
0
Comment
Question by:gosonic
  • 2
4 Comments
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 24418556
Hi gosonic,
>>..MyFramePage.aspx should check to see if it is loaded in MainPage.aspx if it is not MyFramePage should redirect/reload another page in its place.  
Try this:

MainPage.aspx

==================

<iframe id="if1" src="MyFramePage.aspx">

</iframe>
 

MyFramePage.aspx

==================== 

<script>

if(window.parent.frames["if1"]){
 

if(window.parent.frames["if1"].location.toString().indexOf("MyFramePage.aspx")==-1){

 //change source

 location.href='http://google.com'

}
 

}else{

 //not loaded in any iframe

 location.href='http://google.com'

}
 

</script>
 

Text bla bla bla....

Open in new window

0
 
LVL 29

Accepted Solution

by:
David H.H.Lee earned 500 total points
ID: 24418582
Hi gosonic,
Another approach for checking iframe checking inside MyFramePage.aspx:



MyFramePage.aspx

==================== 

<script>

var isLoadedInIFrame = (window.location != window.parent.location) ? true : false;
 

if(!isLoadedInIFrame){

  //not loaded in any iframe

  location.href='http://google.com';

}else{

  //check parent page

  if(window.parent.location.toString().indexOf("MainPage.aspx")==-1){

    //not loaded in MainPage.aspx

    location.href='http://google.com';

  }

}

</script>

Open in new window

0
 
LVL 15

Expert Comment

by:fsze88
ID: 24418588
like this?

<script type="text/javascript">
function checkIframeSrc(){
  var iframesrc = document.getElementById('iframe1').src;
  var iframefilename = iframesrc.substr(iframesrc.lastIndexOf("/") +1   , iframesrc.length-iframesrc.lastIndexOf("/"));
 if (iframefilename != 'iframe1.html'){
  document.getElementById('iframe1').src = 'http://www.yahoo.com';
 }
}
</script>

<body  leftmargin=0 topmargin=0 marginwidth=0 marginheight=0 onload="checkIframeSrc();">
<iframe name="iframe1" id="iframe1" src="iframe1.html" style="visibility:visible; " frameborder=1 scrolling=yes width=500 height=500>
</iframe>
</body>
0
 

Author Closing Comment

by:gosonic
ID: 31582872
Thanks for your help.  The app I'm using it in is IE8 specific and it works just fine there.  FYI : Also tested Chrome & Opera works there too. It didn't test out in FF but that doesn't matter to me at this point.
Thanks again
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now