`
king_tt
  • 浏览: 2108476 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox

 
阅读更多

Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox

http://roneiv.wordpress.com/2008/01/18/get-the-content-of-an-iframe-in-javascript-crossbrowser-solution-for-both-ie-and-firefox/

Ok, let’s imagine the use case: I have an iframe somewhere on my page, and when I click a link or a button I need to get the content of it (could be a textarea e.g.), and then do some stuff with it.

It was easy to do this in IE, but for Firefox I struggled more, as I kept getting the “frame has no properties” error message in the console. And when I solved this I couldn’t get to the content.

There is a lot of references out there claiming that you could use document.frames['nameOfMyIframe'] or window.frames['nameOfMyIframe'] to get the frame, and then use the .innerHTML to get the content, but both are wrong.

I came up with the following function that seems to do the job in both Firefox (tested on version 2.0.0.11 and 3.03 ) and in IE (6 and 7):


function getContentFromIframe(iFrameName)
{

var myIFrame = document.getElementById(iFrameName);
var content = myIFrame.contentWindow.document.body.innerHTML;

//Do whatever you need with the content

}

分享到:
评论

相关推荐

    JavaScript Functional Programming for JavaScript Developers (PDF, EPUB, MOBI)

    If you are a JavaScript developer interested in learning functional programming, looking for the quantum leap toward mastering the JavaScript language, or just want to become a better programmer in ...

    HTML, XHTML, and CSS All-in-one Desk Reference For Dummies

    You′ll learn how to work with Positionable CSS to create floating elements, margins, and multi–column layouts, and you′ll get up to speed on client–side programming with JavaScript. You′ll also ...

    Mastering.JavaScript.1785281348

    Along with HTML and CSS, it is one of the three essential technologies of World Wide Web content production, and is an open source and cross-platform technology. The majority of websites employ ...

    Mastering JavaScript(PACKT,2016)

    Along with HTML and CSS, it is one of the three essential technologies of World Wide Web content production, and is an open source and cross-platform technology. The majority of websites employ ...

    源码+书Hands-on Data Structures and Algorithms with JavaScript

    What you will learnBuild custom Back buttons embedded within your applicationBuild part of a basic JavaScript syntax parser and evaluator for an online IDEBuild a custom activity user tracker for your...

    javascript.the.definitive.guide.5th.2006(英文版)

    This book is both an example-driven programmer's guide and a keep-on-your-desk reference, with new chapters that explain everything you need to know to get the most out of JavaScript, including: ...

    Third-Party JavaScript

    Writing conflict-free JavaScript, HTML, and CSS Making cross-domain requests from the browser How to overcome third-party cookie limitations Security vulnerabilities of third-party applications ...

    Learn HTML5 and JavaScript for iOS

    Get an overview of HTML5, JavaScript, and mobile web frameworks Discover tips for iOS usability as well as performance Dig into features like images, animation, and even geolocation

    JavaScript: Moving to ES2015

    This course offers an expert's eye on the latest ES6 features and how these advanced tasks fit together in JavaScript as a whole Discover robust JavaScript implementations of classic and advanced ...

    Wiley.JavaScript.Bible,7th.Edition

    over fifteen years of daily work in JavaScript and a constant monitoring of newsgroups for questions, problems, and challenges facing scripters at all levels. Our goal is to help you avoid the same ...

    Javascript.Object.Oriented.Programming.pdf

    You will discover the advantage of duck typing in both Python and JavaScript, while you work with interfaces and generics in C#. With a fair understanding of interfaces, multiple inheritance, and ...

    Firefox and Thunderbird Garage

    According to spreadfirefox.com, to-date there have been ~ 23 million downloads since the release of Firefox 1.0 in September 2004, and MS IE usage has been dropping at the same time. Thunderbird has ...

    Beginning JavaScript with DOM Scripting and Ajax: Second Editon

    Beginning JavaScript with DOM Scripting and Ajax is an essential resource for modern JavaScript programming. This completely updated second edition covers everything you need to know to get up-to-...

    Design of Experiments for Engineers and Scientists-Elsevier (2014)

    Design of Experiments for Engineers and Scientists Second Edition Design of Experiments (DOE) is a powerful technique used for both exploring new processes and gaining increased knowledge of existing ...

    Pro Windows8 development with HTML5 and Javascript

    The most important features are given a no-nonsense in-depth treatment and chapters contain examples that demonstrate both the power and the subtlety of Windows 8, Windows Runtime and Javascript. ...

    The Majesty Of Vue.js Paperback – October 28, 2016

    and that its integration can save you a lot of time and effort This book will guide you through the path of the rapidly spreading JavaScript Framework Vue.js Who This Book Is For This book is for ...

    JavaScript.The.Definitive.Guide

    This book is both an example-driven programmer's guide and a keep-on-your-desk reference, with new chapters that explain everything you need to know to get the most out of JavaScript, including: ...

    Beginning JavaScript with DOM Scripting and Ajax (pdf + ePub)

    Beginning JavaScript with DOM Scripting and Ajax is an essential resource for modern JavaScript programming. This completely updated second edition covers everything you need to know to get up-to-...

Global site tag (gtag.js) - Google Analytics