-1

I have gotten this to work earlier, but for some reason it's no longer working. Could someone please explain me why this won't work now?

Page one that contains the iframe:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
    <head>
        <title>Test 1</title>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    <body>
        <div id="thediv" style="background: #ffff00;"> div </div>
        <iframe id="theframe" src="test2.html"></iframe>
    </body>
</html>

the iframe content

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
    <head>
        <title>test 2</title>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script>
           $(document).ready(function () {
               $("#clickme").click(function(){
                   $("#thediv", top.document).css("background","#ff0000");
                   alert($("#thediv", top.document).text());
               });
           });
        </script>
    </head>
    <body>
        <a href="#" id="clickme">clickme</a>
    </body>
</html>
Wouter J
  • 41,455
  • 15
  • 107
  • 112
Scott
  • 1
  • 1
  • 1
  • ok with more testing it appears that the iframe is acting like the top document.. not sure why yet. – Scott Dec 16 '12 at 15:50
  • ok if I add a div to the iframe content with the ID of "thediv" the code reacts to that div instead of the top.document... – Scott Dec 16 '12 at 16:00

2 Answers2

0

This is working fine in Firefox 9.

Keep in mind that .click() won't work for an element added dynamically.

Update: This is not allowed due to security reasons. Please see this thread: Unsafe JavaScript attempt to access frame with URL

Community
  • 1
  • 1
ATOzTOA
  • 34,814
  • 22
  • 96
  • 117
  • you are correct that this does work in ff9 however it does not in chrome... this is in the same domain so it shouldn't violate any security.. I currently run something similar on another site where I use top.document within an iframe to hide a div in the parent which works in chrome... this is why I am confused – Scott Dec 16 '12 at 21:36
  • Chrome does display the security warning in the java script console. – ATOzTOA Dec 17 '12 at 03:45
0

ok I couldn't get this to work on my local machine however on my live file I was using a full url in the iframe src path even though it was in the same domain it didn't work... after removing the url path it seemed to work... thanks anyway

Scott
  • 1
  • 1
  • 1