Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
618 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

671 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