Get the url from frame redirection - javascript

I did a frame redirection to redirect subdomain.domain1.com to domain2.com. It works well ie I got "subdomain.domain1.com" in the address bar and the domain2.com is well displayed. However, I try to get the url in js code.
console.log(window.location.href);
This line show me "domain2.com" but it would be "subdomain.domain1.com". How to get that ?

window refers to the current page.
If you want the URL of the parent frame then you would need to go to window.parent.
At that point you'll run into a cross-origin error. If you controlled the parent then you could use postMessage to pass the data through.
Since you are using a frame redirection, that seems unlikely though. Generally this is one of many limitations with frame based redirection that mean you should generally avoid that approach in the first place.

Related

Setting source again refreshes iFrame?

I am using the below to set my iFrames content, but will setting the same url a second time refresh the content in all browsers? Or do I need to use http://targeturl.com/targetpage?random=currenttime to prevent caching and cause a reload?
function setIframeSource(Iframe, targetUrl){
var Elem = document.getElementById(Iframe);
Elem.src = targetUrl;
}
it will definitely reload the page, but caching might be handled differently depending on the browser/version/server and even user settings, so if you want to refresh it properly - use cachebuster (time).
You shouldn't need to force a cache refresh like this on web pages, unless you know that that particular web page needs a forced cache refresh.
If you needed to append a cache refresh querystring to every web page in an iframe, then this would be true for accessing any web page in your browser, which obviously isn't the case.
I believe it shouldn't refresh the iFrame if the URL is the same or only the fragment identifier (# part) changes, but you might need to test this on a few browsers (particularly IE which never seems to follow specs)
If you are setting the URL with Javascript, why don't you call the reload() method of the iframe? Eseentially, that iframe is a window, and contains the same methods you would have for a normal window object.

Update URL from inside an iFrame

is it possible for javascript inside an iFrame to update the URL (hash) of the parent page (and retrieve it)
Does it have any permissions?
To further explain, I have no hosting for this domain, I can only set up an Iframe. I also cannot use a DNS config to get that page to display because of limitations of my hoster.
I also cannot transfer the domain to them to make that work because my clients wants to keep control of the domain.
Thank you for your help!
If the <iframe> page is within the same domain, probably yes. Otherwise you don't get access to the parent page due to cross-domain restrictions.
You can change the URL of the parent page though:
top.location.href = 'http://www.example.com';
due to security constraints you will not be able to access properties of the parent window IF the domain,port or protocol is different than the one in the iframe.
To be short, the answer is NO.
Your script works only inside the context of that iframe.
If you try for example,
var loc = document.location;
you will see what I mean.
One solution is that when you give the other side your iframe, you should add a script in witch you can do whatever you want, because it runs on their domain.
Maybe dynamically create the source of your iframe and stuff.

Call the function only after the browser is fully redirected?

<script>
function test() {
alert("this should only be called after the browser is fully redirected?");
}
window.location = "http://google.com";
test();
</script>
I'm about redirecting the user guys to a page and I want to do something (call a function) only after the browser is fully redirected but I can't get it to work. Is there any way for me to do so?
Is there any way for me to do so?
Nope. When the page has opened google.com, you no longer have any control over the browser window.
Once URL changes, all execution of the current page is stopped.
Not really. You'd have to put the page you were redirecting in a frame and keep the script in another frame, then watch for the content frame to get updated. But you'd also run into cross-domain issues because of the Same Origin Policy (which governs access to one document's contents [the new page] from another document [the one containing the script you're running]). So basically, you can't do this.
If you post a separate question saying what you're trying to achieve by running more code afterward, it may be that people can help you with alternative approaches.
i don't believe this would work. it's a form of XSS/injection and therefore a security risk. i don't think the W3C allowed this sort of thing because it's very dangerous. as soon as the user is loading a different page, the browser ignores the previous one.
http://www.coderanch.com/t/439675/HTML-JavaScript/Javascript-call-AFTER-redirect
see that guy's answer for a visual

Permission issues checking if parent site is my parent domain within iframe

I've read several of the questions on this but am still a little confused.
For example: OK, I can't post examples because of hyperlink limitations
Here is my exact situation.
I have a site at mydomain.com
One of the pages has an iframe to another page at sub.mydomain.com
I am trying to prepare an onload script that if the page is not in an iframe or the parent domain of the page containing the iframe is not mydomain.com then redirect to mydomain.com.
After the initial permission issues I realised the problem with sub domains counting as separate domains.
One of the posts above says that "could each use either foo.mydomain.com or just mydomain.com"
So I tried (for testing):
onload="document.domain='mydomain.com';alert(parent.location.href);"
This produced the error (http replaced with lar
Error: Permission denied for <http://sub.mydomain.net> (document.domain=<http://mydomain.net>) to get property Location.href from <http://mydomain.net> (document.domain has not been set).
Source File: http://sub.mydomain.net/?pageID=1&framed=1
Line: 1
Removing the alert produces no errors.
Maybe I am going about this the wrong way since I do not need to interact with the parent just read its domain if there is one.
A nice simple top.domain. For read only there must be a way so that people can prevent their own pages being used within other people's sites.
You can't (easily) do this because of security restrictions.
This answer from #2771397 might point you in the right direction.
OK, while looking at the error console I still had open when I got home a wee lightbulb lit up. I am pretty new to javascript (can you tell ;) but I thought "If it has try/catch"...
well here is a hack at least to get the name of the top domain and an example of how I will use it in my site to show content only if the page is a frame in the correct domain.
Firstly the header will have the following partially PHP generated function:
function getParentDomain()
{
try
{
var wibble=top.location.href;
}
catch(err)
{
if (err.message.indexOf('http://mydomain.com')!=-1)
{
createCookie('IAmAWomble','value')
}
}
}
Basically the value will be something based on the PHP session I think. This will be executed at page load.
If the page is not within the proper site or if javascript is not enabled then the cookie will not be created.
PHP will then attempt to read the correct value from the cookie and show the content or an error message as appropriate.
I do see a slight flaw in this for first visit since page load will run after PHP has generated the content but I'm sure I can work around this somehow. I thought I'd post because this is at least what I was initially asking for and that is a way to read the URL of a parent site if it is in a different domain to the site in the frame.
IIUC you want to use the window.parent attribute: “A reference to the parent of the current window or subframe.”
Assumably, window.parent.document.location.host contains the container page URL domain name.

How can I call a function in an iframe's parent using Greasemonkey

I have a Greasemonkey script which adds an iframe to the page (call it Page 1). The iframe contains another page (call it Page 2). The script runs on Page 2 as well. Pages 1 and 2 are on different domains.
I'd like to allow code running in Page 2 to call a function on Page 1. Given the lower restrictions on Greasemonkey code, is this possible?
The browser will prevent this, because the domains are different.
There are a couple of tricks you can use to communicate between frames:
Add a DNS record for the other website to the outer website's domain (www.somewebsite.com and someapp.somewebsite.com). Then use document.domain = "somewebsite.com" in both pages' JavaScript.
Use HTML 5 postMessage() to communicate between frames. I know it works in Firefox 3 and Internet Explorer 8, but not in IE7.
You can pass simple messages to another page by setting the parent window's URL.
Note: It appears browsers prevent setting of the parent URL. This method will this only work for one-way communication from parent to child frame.
Ad 3:
You won't be able to read the other frame's URL, but you can set it. If you change the URL to the exact same page, but with an #anchor component to the URL, the page will not actually reload:
window.frames["childFrame"].location.href = "http://www.somewebsite.com/#message"
You'd then need to add a script to the outer page that regularly polls it's location.href and process the messages. Yes, it's ugly, but if done right, it will work in all common browsers.

Categories