By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,599 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,599 IT Pros & Developers. It's quick & easy.

problem with getElementsByName

P: n/a
Hi, I cannot figure out why everything from line

var elem = doc.getElementsByName('keywords')

in x.js is not executed. My goal is to set value of <input
name=keywords ...which is in a.html after page is loaded.

------------------ search.html --------------------

<html>
<head>
<title>Search</title>
<script src="x.js"/>
</head>

<frameset rows="*" onload="load()">
<frame src="search-1.html"/>
</frameset>

</html>

------------------- search-1.html -------------------------

<html>
<head>
<title>Search</title>
</head>
<body>

<iframe src="a.html" height="600" width="600" id="frameSearch">
</iframe>

</body>
</html>

----------------- a.html --------------------------

<html>
<head>
<title>Web Interface</title>
</head>
<body>
<form action="submit">
<input type=text name=keywords size=40 value="">
</form></html>

-------------- x.js -------------------------------

function load() {
var frame = frames[1].document.getElementById('frameSearch');
var doc = frame.document;
var elem = doc.getElementsByName('keywords');
alert (elem.length);
Jul 14 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
wrote on 14 jul 2008 in comp.lang.javascript:
Hi, I cannot figure out why everything from line

var elem = doc.getElementsByName('keywords')

in x.js is not executed. My goal is to set value of <input
name=keywords ...which is in a.html after page is loaded.

------------------ search.html --------------------

<html>
<head>
<title>Search</title>
<script src="x.js"/>
</head>

<frameset rows="*" onload="load()">
<frame src="search-1.html"/>
</frameset>

</html>

------------------- search-1.html -------------------------

<html>
<head>
<title>Search</title>
</head>
<body>

<iframe src="a.html" height="600" width="600" id="frameSearch">
</iframe>

</body>
</html>

----------------- a.html --------------------------

<html>
<head>
<title>Web Interface</title>
</head>
<body>
<form action="submit">
<input type=text name=keywords size=40 value="">
</form></html>

-------------- x.js -------------------------------

function load() {
var frame = frames[1].document.getElementById('frameSearch');
var doc = frame.document;
var elem = doc.getElementsByName('keywords');
alert (elem.length);
Treat frame as a reserved word, use myFrame or so.

Methinks it is document.frames[0] not [1]

The frame being loaded does not sigmal that the iframe is loaded.
Better put some code in the iframe itself,
or use a timeOut delay [not relyable, loading could be interupted].

Extensive debugging will probably give you all the answers,
delete or // the alert()s one by one to reveal any timing problems:
<input name='keywords' value='myTest'>
function load() {
var myFrames = document.frames;
alert(myFrames);
var myFrame = myFrames[0];
alert(myFrame);
var myIFrame = myFrame.document.getElementById('frameSearch');
alert(myIFrame);
var elem = myIFrame.document.getElementsByName('keywords');
alert(elem);
alert (elem.length);
alert (elem[0]);
alert (elem[0].value);
};
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 14 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.