getting a div from an iframe pure javascript - javascript

I am using this piece of code to copy the value of on div to another. The problem is that the "listprice" div is inside the iframe that is being loaded by the page that has v4-25 in it. Do you think there is a way to actually acces the "listprice" from that iframe?
I can't use any library for that, it has to be pure javascript.
var string = document.getElementById('v4-25').innerHTML;
document.getElementById('listprice').innerHTML = string;
Thank you,
H

window.frames[framename].document.getElementById("listprice")

See this question How to Access a DIV inside a iframe from a Parent See this answer, it demonstrates how to access the element across an iframe using plain JavaScript.

Related

JavaScript variable in text input value

I have a main html page which has a text input field and iframe in it .
I would like to be able to click a link in the iframe and and have the input text box value change to the value of a variable called selectedText.
Im not sure how to get it to work . I have been able to get it to work only from the main page by using this :
newListNote.value = selectedText;
As you cannot set the textbox value of parent page from iframe directly,
create a javascript function in your main page like this,
function setValue(val) {
document.getElementById('newListNote').value = val;
}
And call this function from the iframe page like this,
parent.setValue(selectedText);
I consider your textbox has id newListNote. Hope it works, thanks.
I think this one is related to Same-Origin Policy and Cross Site Script policy. You can't achieve it (correct me if i'm wrong).
Assuming your using jQuery as you tagged it and the iframe is another page within your site, it would be better to use .load()
$( "#result" ).load( "ajax/test.html" );
This then loads the page into your document where you can access the links. Then you can achieve your original goal. If the page is external to your site i'm not sure that this would work.
Good luck
A combination of the two answers here by #balaG and #Gereltod are correct.
As #Gereltod suggested, you cannot run JavaScript within an iframe from outside the iframe. If you could, then there would be serious security problems with the internet!
As #BalaG suggested, if you want the website to change based upon an event within the iframe, you need to write code within the iframe, and prepend it with parent.

Accessing a DOM attached function in an iframe from the parent window

Here is my problem:
an iframe is opened in a parent window;
a function loaded in the iframe (js file) is used to modify a DOM located in the iframe;
The function is working perfectly when used in the iframe:
$(DOMId).function();
I am trying hard to do the same from the parent window but without success...
I always get xxx.'function' is not a function.
I tried to access directly functions and variables from the loaded script without problem. For instance:
jQuery(iframeId)[0].contentWindow.direcfunction();
But I can not find the way to call a function attached to a DOM in the iframe.
I tried many things, the latest being:
var iframeDOM = document.getElementById(iframeId).contentDocument.getElementById(DOMId);
jQuery(iframeDOM).function();
Has anybody a solution to this?
I finally solved it. The query from A. Wolff is the correct one:
document.getElementById("iframe").contentWindow.jQuery("#picker").colpick({...})
There was a last problem due to the way colpick works. It prevented me to make it work immediately.
Thank you all for your help.
I am not sure with JQuery (although should be easy), but what happens if you try the following plain javascript ?
document.getElementById('yourIFrameID').contentWindow.yourFunctionInsideYourIFrame();

Style content inside Google Custom Search iFrame?

I'm trying to style a specific div (#cse-hosted) which is the first div inside the body of the Google CSE iFrame. I know you can use the following to set a width for the iFrame using JavaScript variables:
var googleSearchFrameWidth = 'xxx';
But is there anything like that which allows me to go inside the iFrame and style the divs?
I've been searching the web and have found no solutions so there exists the likelihood that there is no way to do what I'm hoping, but...
Anyways, I'm sorry to be so abrupt, but I'd prefer not to give away any other content if at all possible...
You cannot style content that is not on your current page (without javacsript), and you cannot use javascript cross-domain. Unless you have access to the code for the iframe, you aren't going to be able to do much.

iframe content loading [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Getting contents of iframe
Javascript: I need two examples.
Get content of loaded iframe which src is not on same server as parent page.
Get content of loaded iframe which src is ON the same server as parent page.
By content I mean InnerTEXT or innerHTML, or anything. Goal is to transfer variable from one page that is not on same server to other. This was my first thought. If there is any other way to transfer variable within javascript.
Sounds like homework. What have you tried so far? Are you allowed to use jQuery? Show us your examples.
EDIT:
Since you are allowed to use jQuery try going down this path for your solutions:
$('#iframe').contents().find('input').val();
It is not possible to transfer a variable from another website. But you can call functions which are defined in parent.
You make this function on your main site:
function helloWorld(text) {
alert(text);
}
And you call from inside of the iFrame with parent.helloWorld('hiho!');. This should work with the same or any different domain, but you need to access the different page, to add the method call!

DOM window wont work

Im using DOM window ( http://swip.codylindley.com/DOMWindowDemo.html ) to create alerts. However the content that goes in the windows is under the id inline content.
However the js file doesnt do anything with this id and the content inside isn't showing up.
Is there a css file im missing because the window appears but with nothing in it?
p.s. I'm using example one
Unless you absolutely need to use this DOM Window plugin, I'd recommend just using simple javascript or jquery commands to achieve this rather simple task.
But anyways, since you're using example one, you need to make sure that the href on the a tag that you use for your Open DOMWindow function is pointing to the div that houses the content.

Categories