?
Solved

detect parent window URL (and redirect)?

Posted on 2003-02-22
16
Medium Priority
?
609 Views
Last Modified: 2007-12-19
I have a site which is currently displaying within a frameset (cloaked forwarding of the domain by the domain registrar). For reasons which are too boring to go into, I need certain browsers to take the page out of the frameset to be viewed. (for testing, I'm using IE 5.1)

I'm doing well with my browser detect (using a great script I found at http://www.dithered.com/javascript/browser_detect/index.html, but I can't seem to detect whether the page is being displayed within a "parent" window. I've tried the script:

if (window.parent.location.href == "http://www.skmotorsport.com")
     {window.parent.location.href = "http://www.skperf.com";}

But it simply didn't work.

I wondered whether I wasn't getting the parent window object properly addressed, so I made a simple test script that would tell me the parent window's URL:

alert (window.parent.location.href);

This didn't work either in the frameset context. But it DID work when I right-clicked and opened the frame in a new window.

Bang! Up comes the alert message with the URL.

So then I wondered whether it was because the frame the page was inside was being created by the domain registrar, whether that affected the script. So I created a frameset within my same domain, with the page inside that I'm hoping to "free."

Voila! My alert script worked perfectly, displaying the URL of the page. (the redirect still doesn't, though)

Is this a security issue? You can ONLY reference the frameset's attributes from a page inside it when the page and frameset are within the same domain? (the question at hand)

Is there any other way to do this?
0
Comment
Question by:gheffron
[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
  • 6
  • 4
  • 2
  • +3
16 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 300 total points
ID: 7998130
Your assumption is right: it is a security issue.
You can not change nor even read attributes from other domain.

Oh, and avoid reserved words for your object names.
You named the frame "top"
The name "top" is reserved for frames root.

0
 

Author Comment

by:gheffron
ID: 7998153
Thanks Zvonko. It's what I suspected.

But if anyone else has any insight about how to get around it, I'd love to hear. There are reasons why I cannot easily change this frame situation.

I'll wait a day and then assume Zvonko is the winner (and me the loser--sigh).
0
 
LVL 9

Expert Comment

by:sparkplug
ID: 7998582
Hi,

Do you need to do this based on the url of the parent page? If not then you can use the following script which detects if the page is within a parent window and if so breaks you out it.

<SCRIPT LANGUAGE="JavaScript">
<!--
     if (top.frames.length!=0)
          top.location=self.document.location;
//-->
</SCRIPT>

>S'Plug<
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 4

Expert Comment

by:jonnal
ID: 7998587
hi
if you want to change only in certain browsers
why not
if(browsers){top.location="http://www.skperf.com"}

or am I not getting this

jonnal
0
 
LVL 5

Expert Comment

by:msa2003
ID: 7999490
The very easy way is to use document object model.

The parent HTML will create some variable as follows:

<html><head></head>
<body>
<script>
if ((top.a) || (top.aaa == "1")) {
 document.write('<h1>Inside the frameset</h1>');
} else {
 document.write('<h1>Outside the frameset</h1>');
}
</script>
</body>
</html>

Actually all variables in JavaScript are the properties of 'window' object, so in the child window we coult simply check this value:

<html><head></head>
<body>
<script>
if ((parent.aaa) || (parent.aaa == "1")) {
 document.write('<h1>Inside the frameset</h1>');
} else {
 document.write('<h1>Outside the frameset</h1>');
}
</script>
</body>
</html>

Good luck ;-)
0
 
LVL 4

Expert Comment

by:jonnal
ID: 8000212
mmm
this ms article pretty much discribes it
checkout extending the domain

http://msdn.microsoft.com/workshop/author/om/xframe_scripting_security.asp

jonnal
0
 

Expert Comment

by:reubenbell
ID: 8000418
try using opener. this will give the parent of the popup.

so opener.location.href, may work.
0
 
LVL 5

Expert Comment

by:msa2003
ID: 8002302
reubenbell: opener property exists only if window was opened with window.open() method.

qheffron: Important notice: location is a property of a document object, so you might use parent.document.location.href instead of window.parent.location.href (window is assumed by default).

Sorry, there was an mistake in my previous comment:

parent HTML:

<html><head>
<title>The sample frameset</title>
<script language="javascript">
aaa="1";
</script></head>
<frameset><frame src="frame.html"></frameset>

child HTML:

<html><head></head>
<body>
<script>
if ((parent.aaa) || (parent.aaa == "1")) {
  parent.document.location.href = "http://www.skperf.com";
}
</script>
</body>
</html>

I hope this is exactly what you need.
0
 

Author Comment

by:gheffron
ID: 8011268
I tried sparkplug's solution but it didn't work -- probably for the same reason as Zvonko mentioned (security doesn't allow you to read properties for pages in other domains that where your HTML is located -- frame in skperf.com while parent is skmotorsport.com). Ditto for jonnal's idea, though I'll check out that link.

msa2003's solution doesn't work because I'm not writing script for the frameset/top window (created by the domain registrar with "cloaked forwarding" (i.e. they stuff my actual IP into a frame they create).

With all the good (if incorrect) ideas, I'll wait another day before I list Zvonko's as the correct answer.

Thanks everyone for your effort!

0
 
LVL 4

Expert Comment

by:jonnal
ID: 8011582
hi
cant understand how your page cant check which browser it is in eg
do your browser detection and then

if(browser == netscape){  // or whatever
top.location = "http://www.skperf.com "
}

are you saying cloaked forwarding stops you changing top.location?? from inside the page!

or am I not getting this

jonnal
0
 

Author Comment

by:gheffron
ID: 8011894
jonnal --

Zvonko is saying that because the top/framset page is within another domain, I can't access the "location" property of that page. (hence your code not working)

I'm assuming he's correct, because I know there security restrictions built into javascript to prevent people tweaking with another site's pages.

For example, if you put another site's page inside a frame, like here http://www.nano-stock.com/quotes.html (bottom frame is another site's service), you wouldn't want that other site to be able to break your users away from your domain and redirect them to theirs.

Also, I think Zvonko is correct because your script (and others I've tried) work perfectly when I create a frameset with my child domain. I can access all the properties of that parent page (location, size, etc.), and redirect my user anywhere I want.
0
 
LVL 4

Expert Comment

by:jonnal
ID: 8012945
yes gheffron
I understand that

but you said
"I need certain browsers to take the page out of the frameset to be viewed"

so if the viewer is using one of those browsers
why not just redirect !!

no reading top.location or anything else
just redirect on browser used

and now for nmy catch-fraze
or am I not getting this

jonnal :)

0
 

Author Comment

by:gheffron
ID: 8013261
jonna --
   The issue is which page can you redirect? You can't redirect the frameset page because you cannot access it's location property (e.g. top.location = "http://www.skperf.com";)

I suppose you could open a new window which goes to skperf.com, but you couldn't close the parent window.

Hmmmm. Worth a thought. You could open an entirely new window with the correct URL, while redirecting the child window to a blank screen.

In fact, that would just do it! I'm going to try it now.
0
 

Author Comment

by:gheffron
ID: 8013498
It worked! Not the most elegant solution, but a workable one for now. IE6 users will get a new window at http://www.skmotorsport.com which opens to http://www.skperf.com. The bottom window then directs to a blank page. Everybody else goeth nowhere.

I'll see if i can split points for Zvonko and jonnal.

Thanks again, everyone!

gheffron
0
 

Author Comment

by:gheffron
ID: 8013506
This was the root of the issue. Thanks again!
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 8014726
In spite that I brought bad news is the regular grading an A.
Give a B only if you are disappointed by expert, not by the technical opportunities.

This is only for the next time.

See you,
Zvonko

0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

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
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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

777 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