1

I am trying to access an element in an iframe and .find() is not able to select any element. If I access .contents[0] I do get the entire document but if I try .find( "*" ) or any other selector it returns an empty object. Both html file are in the same folder.

var iframe_all = $('#iframe_vote').contents().find( "*" )
var iframe_document = $('#iframe_vote').contents()[0]

console.log(iframe_all)
console.log(iframe_document)

Result:

Object { length: 0, prevObject: {…} }

HTMLDocument file:///c:/Users/piero/OneDrive/Documents/Visual%20Studio%202017/Projects/marketPredictions/marketPredictions/vote.html
URL: "file:///c:/Users/piero/OneDrive/Documents/Visual%20Studio%202017/Projects/marketPredictions/marketPredictions/vote.html"
activeElement: <body>
alinkColor: ""
all: HTMLAllCollection { 0: html.no-js, 1: head, 2: meta, … }
anchors: HTMLCollection []
applets: NodeList []
baseURI: "file:///c:/Users/piero/OneDrive/Documents/Visual%20Studio%202017/Projects/marketPredictions/marketPredictions/vote.html"
bgColor: ""
body: <body>
characterSet: "UTF-8"
charset: "UTF-8"

HTML:

<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title></title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script type="text/javascript" src="jquery.js"></script>


        <link rel="stylesheet" type="text/css" href="css\vote.css">
    </head>
    <body>


        <a id = 'btnPlus' href="#plus">
            <span class="bg" id="plus"></span>
            <span class="symbol"></span>
          </a>
          <a class="button minus" id = 'btnMinus'href="#minus">
            <span class="bg" id="minus"></span>
            <span class="symbol"></span>
          </a>
          <span class="cancel">
            <a href="#">Clear</a>
          </span>

    </body>
</html>

1 Answers1

1

you will need to setup a server environment to work with iframes, file:/// isnt a valid domain and will cause issues with cross domain validation

remember that both pages need to be hosted on the same domain/server

this is a security measure, here is some more information: https://blog.chromium.org/2008/12/security-in-depth-local-web-pages.html

Tyler Fowle
  • 549
  • 2
  • 13