472,127 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

problem with getElementsByName

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
1 2003
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.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.