views:

27

answers:

2

Hi All, i want to access a javascript function which resides in a script file from another page with iframe. my sample code :

Page from which javascript need to be accessed.

 <iframe id="FRAMESET" src="default.htm" width="0%" height="0%">
        <p>
            Your browser does not support iframes.
        </p>
    </iframe>

default.htm

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <link href="Scripts/main.js" type="text/javascript" />
</head>
<body>

</body>
</html>

main.js

function helloWorld() {
    alert("hello World");
}

i want to access this function on main page. i tried document.getElementById('FRAMESET').contentWindow.helloWorld(); but gave me error "that document.getElementById('FRAMESET').contentWindow.helloWorld();" is not a function.

A: 

It's impossible. All browsers (IE, FF, Chrome) now prevent js accessing to iframe content. Why don't you just load main.js into the main page?

Bang Dao
Actually you can access iframe content, as long as the iframe resides in the same domain.
reko_t
@reko_t: thank for information
Bang Dao
+2  A: 

It's possible. You can do this in the page that contains the frame:

document.getElementById('FRAMESET').contentWindow.helloWorld();
reko_t
thanx for the quick reply. i tried this but its not working .its giving me document.getElementById('FRAMESET').contentWindow.helloWorld(); is not a function
George
It should work. Here's an example: http://reko.tiira.net/frametest/
reko_t
tnx dude.this worked for me .....tnx a lot ............
George