views:

52

answers:

2

I have this simple line

alert(window.parent.frames[0].document.getElementById('textToSearch').value);

I have 2 frames, first with a text field with id 'textToSearch' I want to get the value of the text field in the second frame The line above is on the html file from second frame I get this error only in Google Chrome, in IE or FF works fine.

Uncaught TypeError: Cannot call method 'getElementById' of undefined

Any ideas?

Thanks in advance

+2  A: 

Use contentWindow.document instead of document:

var frame = window.parent.frames[0].contentWindow;
alert(frame.document.getElementById('textToSearch').value);

You can also just use contentDocument for most browsers, but not Internet Explorer 7 or older.

Andy E
Thanks for answer ... but didn't work. i receive Cannot read property 'document' of undefined. If i want to see what is in variable 'frame' with alert, i have 'undefined'
tinti
@tinti: that's strange. There's no *document* property for an iframe element in Chrome, but there are *contentWindow* and *contentDocument* properties. If it's finding the frame, the code I gave you **should** work.
Andy E
I don't know if is a problem, but i use frame instead of iframe. <frameset cols="20%,*"> <frame name="frame1" id="frame1" src="f1.html"/> <frame name="frame2" id="frame2" src="f2.html"/> </frameset>
tinti
@tinti: I meant frame element (force of habit). The properties are very similar. *contentDocument* is defined in the DOM level 2 spec, but all current browsers implement *contentWindow*.
Andy E
Ok, but after your first line i insert alert(frame). It's 'undefined' ... so the second line is senseless. Anyway thanks for your help
tinti
You have other advice for me?
tinti
@tinti: I'm afraid not, I don't see what else could be the problem.
Andy E
A: 

Finally i figure what was the problem. I try the code from above on Google Chrome on a system local file. Due the security settings of Google Chrome this usecase is impossible. If i move all files on a web server this will work as a charm Thanks all for your support, this thread can be closed now

tinti
You are responsible for closing the thread by marking an answer (your own or someone elses) as answered.
August Lilleaas
ok, i will accept this answer tomorrow ... as the suggestion on the tooltip
tinti