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
615 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Suggested Courses

615 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