Link to home
Start Free TrialLog in
Avatar of gheffron
gheffron

asked on

detect parent window URL (and redirect)?

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?
ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gheffron
gheffron

ASKER

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).
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<
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
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 ;-)
mmm
this ms article pretty much discribes it
checkout extending the domain

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

jonnal
try using opener. this will give the parent of the popup.

so opener.location.href, may work.
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.
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!

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
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.
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 :)

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.
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
This was the root of the issue. Thanks again!
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