1

I Have 3 html files & 3 js files

index.html,rank.html,score.html

global.js,rank.js,score.js

In index.html I have included rank.html and score.html using iframe.

In global.js I have declare Var rank; & global.js included in rank.html & score.html.

I am updating the value of rank in rank.js and i want to access the updated value in score.js. In score.js I dont get the Updated value It gives UNDEFINED value.

I dont want to include rank.js in score.html so i created global.js & included in html file.

Here is my code,

Index.html

<html> <head> </head> <body> <iframe src="../rank/rank.html" frameborder = "0" scrolling="no" width="50%" height = "100%"></iframe> <iframe src="score.html" frameborder = "0" scrolling="no" width="50%" height = "100%"></iframe> </body> </html> 

global.js

var rank; 

score.js

$(document).ready( function(){ setInterval(dataupdate, 10000); }); function dataupdate() { alert(rank) }; 

rank.js

$(document).ready( function(){ setInterval(dataupdate, 10000); }); function dataupdate() { rank = "first"; }; 

I want updated value of rank in score.js file how to access that value...

Thanks.....

4
  • Is global.js included in index.html or both score.html and rank.html? Commented Jul 19, 2012 at 8:53
  • Post small code example illustrating your problem. An entire wall of code is hard to understand. Commented Jul 19, 2012 at 8:54
  • It is the matter of ordering your script files. Declare global.js file before you declare any file that wants to access the variable from global.js. Commented Jul 19, 2012 at 8:54
  • ok thanks.... Using parent.rank = "first" & alert(parent.rank) will solve the problem Commented Jul 19, 2012 at 11:16

4 Answers 4

1

When you declare a global variable like you've done it gets created as a property of the window object, i.e window.rank. Your main window and the 2 iframes have separate window objects and therefore window.rank assigned from the main frame is not the same window.rank accessed from one of the iframes.

For accessing the rank property of the main window object from inside an iframe, you can use parent.rank instead of window.rank

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks , It will really useful for me... Thanks
1

If global.js is included in index.html then you need to use window.top.rank (or window.parent.rank if index.html may also exist in a frame).

Comments

0

You should hold the varibale in the index , cause you can only access the parent variable from iframe

index var a=5; ----------- rank parent.a=6 

now score can read parent.a as 6

Comments

0

try

window.parent.rank 

or

window.top.rank 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.