javascript let iframe become parent - javascript

i have one iframe on my page which has a different domain to that of my site. i know i can't access anything inside the iframe from the parent window, but can i make that iframe take over my entire page - ie redirect the parent to the url of the iframe?
so far i have tried things like
window.location.href = iframe.contentWindow.location.href;
but the browser won't allow that - i guess it isn't smart enough to realise that the = assignment is for the purposes of redirecting and not storage for later inspection.
is there another way of doing this on all modern browsers?

How about:
window.location = iframe.src;

Why don't you try with the iframe src attributes?
with jquery:
window.location.href = $('#iframeid').attr('src');
or:
window.location.href = document.getElementById('#iframeid').src;

Related

Read iframe redirect (same domain)

I'm working in writing a chrome extension, and as a result I have the peculiar situation of using non-cross domain Iframes, without the ability to alter the page being displayed in the frame.
When a user clicks a certain link, in he iframe, I want to run some JavaScript. The default behavior for clicking that link is to load page targetpage.com. I don't think it's possible, or easy, to read listen for a particular element being clicked inside an iframe
As a workaround, I figure I can check if the iframe reloads, pointing to targetpage.com, and then perform the action.
Note: the action is entirely in the parent page, let's imagine I'm just trying to trigger an alert box.
I know how to trigger JavaScript when an iframe loads. My three questions are:
1) how can I check the url of an iframe?
2) is there a way to check the iframe, url prior to targetpage.com being loaded. Esther than after?
3) is there a better solution?
You could listen to webNavigation.onBeforeNavigate in background page, it fires when a navigation is about to occur, and you could get url and frameId in the callback parameter.
This may not be the best answer because I haven't played around with this much.
Chrome has a webNavigation API that looks to be something which may come in handy for your extension.
However if you want to get the current domain you're on you'd use...
document.domain
If you're in a subdirectory of that domain you can grab that with...
window.location
It also works with an added hash to the url.
If you want the url without the hash you could use document.referrer or if you feel hacky you could do something like...
var str = window.location
var res = str.toString().split(str.hash)
document.body.innerHTML = res

How can I reload an iframe inside iframe with JavaScript?

I have a domain,
example.com
Iframe's source is,
server1.example1.com
The problem is, I would like to refresh the iframe with JavaScript inside my iframe, when I location.reload() the page, Iframe loads empty and this only happens in chrome, How can I fix this?
perhaps try this:
// Reload the current page, without using the cache
document.location.reload(true);
source: https://developer.mozilla.org/en-US/docs/Web/API/Location.reload
I know Chrome does some pretty clever things with its cache, so maybe that will help.
right click and reload frame, seriousle:
If the iframe was not on a different domain, you could do something like this:
document.getElementById(FrameID).contentDocument.location.reload(true);
But since the iframe is on a different domain, you will be denied access to the iframe's contentDocument property by the same-origin policy.
But you can hackishly force the cross-domain iframe to reload if your code is running on the iframe's parent page, by setting it's src attribute to itself. Like this:
// hackishly force iframe to reload
var iframe = document.getElementById(FrameId);
iframe.src = iframe.src;
If you are trying to reload the iframe from another iframe, you are out of luck, that is not possible.

Complete isolation of javascript in the iframe from the same domain

Assume we have a window and an iframe with some javascript in it. The iframe sourcecode is defined directly in "srcdoc" attribute of the iframe.
Is it possible to somehow force the browser to behave like the iframe is loaded from another domain?
I mean I don't want the javascript in the iframe to be able to access the main window via "window.parent" or anything like that, because the iframe contents is not trusted. But the problem is that it's stored on the same domain and I even want to use the same request to load both the main window and the iframe contents (with that "srcdoc" attribute).
So is it possible at all?
Thanks!
You could prepend the string:
"<script> parent = top = null; </script>"
To the srcdoc. That should prevent the rest of the code in the srcdoc form accessing the parent through window.parent and window.top.
I'm not sure if there are any other ways to access the parent of an iframe.

Redirecting child iframe to relative url, using js within iframe - but url is relative to the parent

Some interesting behavior that I didn't expect. I have a page which contains an iframe, and in that iframe there's a javascript function that redirects its own window.
I call the iframe's js function from the parent frame. My expected behavior is that it will redirect the iframe to a new page, relative to the iframe's existing location.
Instead, it gets redirected relative to the parent frame's location.
You can see a demo here:
http://thedailynathan.com/files/outlink/parent/parent.html
Am I doing something wrong here, or will I just have to code in an absolute url for my redirect?
Found this thread that sounds very similar. However no one came up iwth an answer for it:
Using relative url for window.location in child iframe
Change the:
document.getElementById("myframe").contentWindow.moveMe()
to:
document.getElementById("myframe").contentWindow.location = "javascript:moveMe()"
This way, the moveMe executed in the iframe's context.
Try this one, it works for me.
<script>function RedirectPage(){ document.getElementById("frame1").src = "http://WWW.microsoft.com";}</script>

any way of redirecting the whole page from script inside iframe

As the topic says, Im looking for a way to redirect to another page, when i click a link in the iframe. i dont want just the iframe to redirect, but the whole window.
is this possible?
preferrably in javascript or in asp.net if possible
EDIT: When i try the answers i get redirected to the source of the iframe, not to the source of the site the iframe lies on... Ill show you the code
function redirect() {
window.top.location.href = "./Nyheter.html";
}
As i dont want the code to be static, so that i can use it on many pages without changing the url, i want to do it this way, alt. get the url from db... but preferrably this way, Solution?
Also, forgot to mention. The pages are not on the same domain, they are on different ones.. this could cause some problems.
Click me!
You cannot use the window.top.location if your frame and the top level page are on different domains.
A similar answer.
javascript: window.top.location.href = "xxx"
You should see this question which is pretty much identical: Redirect parent window from an iframe action
I don't believe you can do it in ASP.net because this has to be done on the client side (ie. Javascript).
window.top.location.href = "http://site.com";

Categories