5

I can access local contents loaded in an <iframe> with

$("#frame").contents().find('div').css(...)

When using a <object type="text/html"> instead (same local site), the contents function does not work.

Is there another way or did I miss something ?

Here follows the test code:

HTML:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <link type="text/css" href="css/style.css" rel="stylesheet"/>
    <script type="text/javascript" src="js/jquery-1.3.2-min.js"></script>   
    <script type="text/javascript" src="js/try.js"></script>
</head>
<body>
    <div id="header"></div>
    <div id="here_goes_a_proprietary_side_i_dont_want_to_mess_inside">
        <object id="frame" type="text/html" data="/nastysite/index.php" width="100%" height="100%"></object>
    </div>
    <div id="footer"></div>
</body>

JS:

$(document).ready(function() {

    alert("go ?");  //temporary solution to wait until everything is loaded.
    $("#frame").contents().find('div').css("background-color", "red"); //nothing appens
    console.debug($("#frame").contents().find('div'));  //nothing
});
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Cal
  • 51
  • 1
  • 1
  • 3
  • 2
    If you are already using jquery why dont you just use `$(targetElement).load('/nastysite/index.php');`? It would seem a lot less complex than using the object method. – prodigitalson Dec 30 '09 at 17:58
  • It was my first idea, however this site contains an old version of jquery with a lot of plugins. Off course i tried noConflict() and noConflict(true), without great success. – Cal Dec 31 '09 at 15:44
  • Did you ever figure this out? I have the same question. – Avery Richardson Jul 18 '11 at 18:10
  • Is the iframe loading from a different domain ? I believe JavaScript doesn't allow that. Try your code with an iframe that loads something locally. – sjobe Dec 30 '09 at 17:05
  • It is a local content. – Cal Dec 30 '09 at 17:24

1 Answers1

-1

Your object tag closes immediately, so it has no child div to find.

<object id="frame" type="text/html" data="/nastysite/index.php" width="100%" height="100%"></object>
Jage
  • 7,990
  • 3
  • 32
  • 31
  • While its true he needs to use a full closing tag and not short syntax, im still not sure if it will expose the DOM of the page he is "importing". – prodigitalson Dec 30 '09 at 17:06
  • Tried with the short syntax, but it has no more effect (the site is still loaded successfully). – Cal Dec 30 '09 at 17:30