problem with getElementsByName

B

bbla32

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);
 
E

Evertjan.

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);
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top