Accessing iframe through console using DOM and BOM in JS - javascript

I've been playing around with DOM and BOM in JavaScript through Google Chrome's console trying to change locally input values, colors, etc...on different websites. I stumbled on a website that used iframe tags for it's form. I tried to change the value of the inputs located inside the iframe but couldn't get it done. My understanding is iframe incorporates a separate webpage inside of the one currently viewed.
My question is simple how do I access the tags inside of the iframe?
Things I tried so far:
document.getElementById("")
document.getElementsByTagNames("")
window.frames
window.parent

So an iframe is an inline frame, and what that inline frame contains is the building blocks to build a website, or display someone elses website. If you know DOM traversal you would basically have to select that iframe, and then go to select the DOM objects within that iframe from there to manipulate those.
iframe = $('#myId');
iframe.contents().find('htmlelements');
Here is a sandbox environment to begin understanding the traversal portion:
http://jsfiddle.net/fDFca/

Related

how to get the contents of an iframe that is dynamically placed

I have a program (called 'PersonalBrain') that exports to html using very complicated javascript (based on Prototype). The script places an iframe inside a div in a main html file. The result is that a searchengine cannot index the content of the iframe. Changing the script is not an option. My idea is now to somehow get the content of the iframe and place this inside another div in the main html - in such a way that this is indexable (and then hide it with css). How would I do that? It needs to be in plain javascript (or maybe php?).
BTW: the id of the iframe is also placed dynamically.
A live example can be found on https://www.wideopenwindows.be

Google AMP html - insert amp-iframe without src attribute

I'm trying to run a script inside an AMP page.
There is no page I need to load within the src attribute; my script should inject an <iframe> with the correct src (it is unknown at first load, only received in response to a request made by my script).
Is this possible in AMP?
Disclaimer: I'm open to different approaches to accomplish the same result - injecting an <iframe> with an src attribute within an AMP page.
Thank you
The AMP page cannot contain any javascript in the first place, so this won't work: https://www.ampproject.org/docs/reference/spec.html#html-tags
The only way to achieve your goal is to:
create an iframe with a src attribute pointing to an HTML page you control
in that page load the Javascript that does the work. You can see a similar approach in this example: https://ampbyexample.com/advanced/how_to_create_interactive_amp_pages/
As stated by #ade you can pull this off. Think about it like this.....
You'll have an HTTPS resource that you can hit that will return the blank iframe along with all of the JS code you need to populate the iframe. So basically an entirely functioning page that will be returned to the AMP-IFRAME.
Calling this from the src attribute of an AMP-IFRAME tag will then pull in your page that includes a blank iframe and all of the scripting needed to populate it or manipulate it. So all of your custom code is happening within the AMP-IFRAME tag but all of it's resources live within the embedded iframe tag that the AMP-IFRAME tag pulls in and renders.
We have a custom video player that works very similar to what you are talking about. I created a template that can be hit via HTTPS that returns a page that iframes our video as well as includes all the scripts to play it and manipulate it. It's all contained in a nice neat little package and the only thing required to use the AMP-IFRAME is the script that extends it. Check out all the AMP-FRAME documentation here.
Hope this helps.

Bypass Cross-Domain-Policy to access iframe's HTML with server-side solution

Currently we are trying to access the HTML of (with javascript) dynamically generated html-elements (e.g. to get the url of an image). If the html-elements are generated with javascript only - their is no problem. Just extracting the image url with javascript by accessing the DOM elements. But now we have trouble with an iframe.
This is the situation at the moment:
We include external script-file (www.test.com/script.js) to create a gallery on our website
The script.js generates a new iframe in our website (document.write('iframe code here')); referencing to www.test.com/iframe.html)
The iframe.html contains javascript-code again to generate the gallery by creating serveral dom-elements (divs, imgs, a, p,....)
Now we need to read the src-attribute of these images. With the debugging tool of the browser, it is no problem. Without the iframe, it's also no problem. But with the iframe, because of the cross domain policy of the browsers we can not access the html of the iframe.html with javascript.
One possible solution was to try to get the src of the iframe tag with javascript, call a server-side script to get the html content of the src-url and run the content via eval()-function on the client again.
I hope you have other better ways to solve that.
It is not clear from your question, but if the iframe is being served by your app, you can use postMessage in order to communicate between the iframe and its parent window.

Dynamically add and remove iframe: Google red flag?

I've been looking into using an iframe to sandbox some javascript for a site module. I have one core question before I start to code and test this. Could adding and then removing an iframe from a webpage prompt Google to ban a website?
I'm not even sure if this is possible but here's what I'm hoping to do in a nutshell:
All of this is necessary to ensure comparability of my module with any website. I'll load a hidden iframe on the page. The frame will handle jquery operations & ajax calls for my module. Once the iframe has loaded I hope to store a reference in the parent window to a function in the iframe. I would then like to use javascript to remove the iframe from the page, while maintaining and utilizing the function reference. I'd appreciate insight into the feasibility of this, in addition to my core question.
Thanks,
skibulk
No, google won't take action against you for dynamically creating an iframe. It's done all the time. I'm not sure whether "iframe sandboxing" works on safari though.

jQuery Copy text inside iframe to main document?

Hopefully someone here can help me with this challenge!
I have a parent page which is the checkout page for an e-commerce site. It's run on zencart and for every order placed a table row is generated through ZenCart. I've setup an EACH function which generates an iframe for an artwork uploader for each TR (order) found. The uploader works and the correct number of instances are being generated.
I wanted to avoid an iFrame, but the uploader script I purchased will not permit me to load it directly into the zencart page template, or via AJAX (tried both). There's some kind of major resource/path situation going on when doing it this way... so I've resorted to iframes.
I'm able to call JS on file-upload-complete. At that point I'm trying to capture the name of the filename that was just uploaded and place it inside the TR. The problem I'm running into are permission error when trying to access the iframe contents.
I've tried everything I've come across on this site and many others, so believe it isn't a problem with the selectors/frame selection... Firebug is telling me that I'm getting permission errors when trying to access the iframe, yet they're both on the same domain and the src is being set by a relative path....
I'm completely lost, any suggestions would be appreciated! Thanks!
www.prothings.com/store
Add items to the cart and go to checkout.....
when you want to access main window or window.document from inside an iframe you should change the context by using window.parent
For example when you want to append some text to a div, you should do something like this
window.parent.$.('#theDiv').text('the text');
There is a bug in IE when you run the code from inside the iframe and remove the iframe in between. IE can't run the code in the fly

Categories

Resources