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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET MVC 5- How can I post textarea value using @Html.ActionLink? 8 55
Can't get page to reload 9 18
Table style position to the left 8 25
Glitching Slide Show 19 27
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

749 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