views:

324

answers:

5

In certain conditions, I have a web page that gets opened in an iframe. When it's loaded into that iframe, I need it to set the window location to a resource to download a file (all of this in an attempt to make an update for a GreaseMonkey script... all legit, btw). Unfortunately, when I do:

top.location.href = "http://userscripts.org/...";

I get an error stating that top is null. when using "window" in place of "top", firefox loads my script text into the iframe, but GreaseMonkey is unable to detect it as an update for some reason. Is there another method to setting the window's location that I've missed somehow?

Edit: The users of my script visit a specific business's page (first). My script is loaded into that page using a firefox plugin called GreaseMonkey. My script creates an iframe on the visited page, and loads my page (second) into that iframe. GreaseMonkey then loads my script onto my page, which checks a value on my page to see if the script has been updated. If the script has been updated, I need to navigate my users away from the site that they originally visited (noted as "first"), and to another (third) site (userscripts.org). Hopefully this clears up some confusion.

+1  A: 

The two locations must have the same host name (domain). You might want to be using parent instead of top.

Josh Stodola
The iframe is loaded into someone else's content. I need it to redirect away from the site when there's an update to the script. Which two locations need to be in the same domain? There are three different domains in the mix: the domain navigated to, the iframe that gets injected loads my domain, and my iframe needs to redirect to userscripts.org.
md5sum
The security restriction prevents a page accessing areas from different domains, but an iframe (under usual conditions on popular browsers) is able to force navigation at the top level.
Pool
Right, but I'm getting "top is null". Using "parent" renders the same results as using "top" ("window is null")...
md5sum
The document in the parent frame and the document inside the iframe MUST be on the same domain. You can't use Javascript to access a window/frame from a different domain.
Josh Stodola
A: 

You could try the following (adapted from something I use):

function checkWebPage() {
    try {
        if (top != self) {
            top.location.href = "http://userscripts.org/...";
        } else {
            self.location.href = "http://userscripts.org/...";
        }
    } catch (e) {}
}
Pool
From original question "I get an error stating that top is null." I need it to set the main window location every time, regardless of whether my document is loaded in an iframe, or as the only window contents.
md5sum
This script should work even if top is null.
Pool
If top is null, it doesn't have a ".location" property... hence the error stating that "top is null". It would then always use self, which has the same effect in this case as "window", which just loads my script text into the iframe, unless the visitor is on MY page, rather than the page I'm injecting my iframe into.
md5sum
A: 

I worked several times with iframe, and I found it was safer to work with the state of the document. I will pass a code with an example.

In this example I'm using jQuery, but is not necessary for functionality.

<head>
<script>
(function($) {
 $.documentCtrolState = function(d,f,t)
 {
    try{
      t=(typeof(t)=="undefined")?100:t;
      if (d.readyState=="complete") //the document has been completely loaded, successfully or unsuccessfully
         f(true);
      else
         setTimeout(function(){$.documentCtrolState.call(this,d,f,t);}, t);
    }catch(ex){
         f(false, ex);
    }
 };
})(jQuery);

$(function(){

   $myIframeRef = $(document.myIFrame);
   $myIframeRef.attr("src", "mypage.php");

   $.documentCtrolState($myIframeRef.window.document, ThenLoadIFrame, 100);

});

ThenLoadIFrame = function(state, ex)
{
   alert("State " + state);
}


</script>
</head>
<body>
<iframe src="" id="myIFrame"></iframe>
</body>
andres descalzo
That's fine, and would work well and safely for loading the iframe with my script... but I need the script referenced by the window, not the iframe.
md5sum
+1  A: 

I finally found that GreaseMonkey offers a function "GM_OpenInTab(url)" which opens a new tab in firefox with the specified url. This allows my iframe to open a new tab with the location of the updated script for GreaseMonkey to update. While this isn't exactly the solution I was looking for, it works seamlessly with no errors.

GM_openInTab("http://userscripts.org/...") ;

Thanks to everyone for your input. It's possible that GreaseMonkey somehow sandboxes scripts to prevent that sort of behavior which could be used for malicious purposes.

~md5sum~

md5sum
Could someone explain why this was downvoted?
md5sum
A: 

This is really lame.

var setloc = unsafeWindow; while (setloc.parent && setloc.parent != setloc) setloc = unsafeWindow.parent; setloc.location.href=url;

DrkShadow