Change URL in an iframe using javascript - javascript

I have a in an iframe, that calls a function from the parent page. The function is window.location, however this does not change the url. Is there a way to have the iframe call a function from the parent page, that will cause the iframe to change url? I also had a basic qustion, if I have an iframe, and click on a link that brings me to a new page, does the parent page remain open?
Thanks in advance for your help. Sorry if I sound like a complete idiot, I am new to javascript.
Dave

window.location is not a function, it s an object.
To do what you want, first make the iframe call a special function from it's parent.
parent.sendMeToGoogle();
And in the function (in parent) do something like:
function sendMeToGoogle(){
document.getElementById('iframeID').src="http://google.com/";
}

If what you really need is to change the parent URL, you can use window.top.location.href='http://anotherURL.com' even if they are in different domains, from the iframe page.

I assume that you want to do more in the function of your parent page; if not you can just change the url of the iframe without calling the parent of course...
As for your second question, the iframe behaves like an ebmedded page: you can browse all you want in the iframe without affecting the parent (except of course with javascript calls like the one you want to use), but browse with the parent page and you will lose teh iframe as well.
Hope that was the explanation you were looking for :)

Related

How to pass values from HTML webresource to javascript on window close MSCRM

I am opening HTML webresource using Xrm.Navigation.openWebResource but on closing of HTML window I want to pass values from HTML to javascript file from where it is opened. Is there call back function can be implemented?
If I open HTML window using window.open I can call parent javascript function using window.opener.functionname on close but click but I want to know how I can pass values to parent javascript file on close button click of HTML window.
I tried with window.parent.opener.Functionname() but it is not working - getting functionname is undefined but it is defined in parent javascript. Pls suggest.
If you're using the 'old' (as it not the unified interface) user interface with turboforms enabled then the parents javascript is actually in a extra iframe called customScriptFrame, and not on the parent itself.
To call something on the parent you can use
parent.customScriptsFrame.functionname() for IE
and
parent.customScriptsFrame.contentWindow.functionname() on chrome.
On the unified interface its much the same, but far more troublesome.
Now the scripts are in a iframe called ClientApiFrame_[n] where [n] is some random number. And i haven't found a good way to determin that number ahead of time from a webresource.
You could go over all frames of the parent in javascript (parent.frames) to find one that has a id that starts with ClientApiFrame_ but that will throw errors trying to read frames with sources set to external domains, and i dont think is very good practice.
Another possibility is registering the function you want to call with the parent ahead of time. so in the main javascript use this.
parent.functionname = functionname
And then from the webResource you can use the normal
parent.functionname
If the webresource is embedded in the form, then use window.parent
If you Xrm.Navigation.openWebResource to open it, then use window.opener

JavaScript in iFrame modify objects out of the iFrame

I have a page with an iFrame embeded. Now I want to modify a object (for example a Button) outside this iFrame by clicking on a button inside the iFrame (with Javascript).
How does this work? Can I just do it like usual?
You can access the parent window using window.parent. From that point, you should be able to do it like usual.
Example :
$('#yourButtonId', window.parent.document).
See also how to access iFrame parent page using jquery? as the question has already been asked

Iframes, child pages and functions

I have an aspx page with some controls. One of which is an iframe, whose source changes (depending on the selection in a listbox). So, if a user selects an item in the listbox (ie: Claims.aspx), the iframes source changes to Claims.aspx.
I have a button on my 'child' pages (ie: Claims.aspx). I'd really like to have that button execute either:
Javascript from the 'parent' page
A VB function in the code-behind of the 'parent' page
Is there any way to do this?
If your parent page and your content page are both on the same domain (which I'm presuming they are) you can simply do:
parent.myFunction();
from your content page to access myFunction() in your parent page.
Similar to Town's answer, you can also use top as in
top.myFunction();
to call the parent's myFunction function from the Iframe.

how do i use parent.frames [] to refer one webpage to another?

i believe that i have to use an array but im not sure where it should be placed and how it should look. im working with javascript and am kinda new at it. should the array be in the tags? or the tag? or should it be inside of the tags but outside of the other two? im lost. should the array be written in just the parent file or the child file? or both? i have created two different pages and now i want the button on one page to return the other page and the button on the child page to close the window. which i think involves the close() option since i used the open() to bring up a new window.
It is unclear what you mean. The way one window accesses another window's properties depends on how they relate; from "child" frame to parent, from parent to "child", from "child" to "sibling"...
If you want to access another frame from within a frame, you're probably best off giving the other frame an id and using something like parent.getElementById("yourId").contentWindow. This will give you the other frame's window object, on which you can call close().
I think this link explains how to access windows, frames and parents.

How to access main window element from iframe

I hope someone can help with this issue. I have a popup iframe using lytebox. I would like it that when I click a link in my iframe with the class mylink that it executes the AJAX call and update a div in my main window with the id="mydiv".
I know how to do the onclick events and the ajax call but I do not know how to access the mydiv from the main window and update it with the content.
Any help on this would be greatly appreciated. Thanks.
You can simply use window.parent.document to access the parent document from the iframe.
jQuery allows you to specify a context for searches, so you could then do:
$(myselector, window.parent.document);

Categories