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
608 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

705 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

19 Experts available now in Live!

Get 1:1 Help Now