<script type="text/javascript">
// <![CDATA[
function initpage() {
document.forms[0][0].focus();
}
// ]]>
</script>
My xhtml validates as strict. I'm using <body onload="javascript:initpage()">, nothing happens
In fact, anything with onX="javascript:initpage()" doesn't work, although javascript:alert('abc') will
If I rename the file to .html, it works!
Yes, I know I can put onX="document.forms[0][0].focus()" directly, but that isn't the point. I'm using Firefox
Thanks already
[web] quickie, javascript and xhtml
Use
EDIT: I see that's not the problem here. Sorry.
Tried the web dev forum?
<body onload="initpage()">
EDIT: I see that's not the problem here. Sorry.
Tried the web dev forum?
You can't have multi-dimensional forms on a HTML page. Try this:
EDIT: javascript does not even support multi-dimensional arrays.
<script type="text/javascript"> // <![CDATA[ function initpage() { document.forms[0].elements[0].focus(); } // ]]> </script>
EDIT: javascript does not even support multi-dimensional arrays.
I would recomend against putting javascript inside html files. Instead, put it in a .js file and include that. To get your initialize event, do
I recomend Quirksmode for all javascript and CSS stuff.
window.onload = initialize;function initialize(){ //Stuff}
I recomend Quirksmode for all javascript and CSS stuff.
Try giving your form and element an id tag and reference them like document.forms['form_id'].elements['element_id'].
the document.forms[0].elements[0] works if the statement is put directly into the onload
I tried (in the script)
and in the page
and that won't do anything, but if I change it to javascript:alert directly, it does work.
I tried (in the script)
function go() { alert('x'); }
and in the page
<button onmouseover="javascript:go()" />
and that won't do anything, but if I change it to javascript:alert directly, it does work.
First of all, you can leave out the javascript: protocol for onX handlers because those are targeted specifically at javascript.<br><br>Secondly, do you have a link to the complete page?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <link rel="stylesheet" href="style.css" /> <title>Testing</title> <script type="text/javascript"> // <![CDATA[ var agent = navigator.userAgent.toLowercase(); var is_ie = ((agent.indexof("msie")!=-1) && (agent.indexof("opera")==-1)); function initpage() { document.forms[0].elements[0].focus(); alert(agent); } // ]]> </script></head><body onload="javascript:initpage()"><br><br><div id="oldfix"><br><div id="central"><br><form id="sendmessage" action="listen.pl" method="get"><p><br> <textarea name="message" rows="20" cols="40"></textarea><br> <br /><br> <input type="submit" value="Send"/><br></p></form><br></div><br></div><br><br></body><br></html></pre>
javascript is case-sensitive and there was a problem with toLowerCase() and indexOf() in your code. Because of those errors your function did not run. I suggest you use Firefox for javascript development, it has an excellent javascript debugger. <br><br>The code below works in IE and Mozilla.<br><br><!--STARTSCRIPT--><!--source lang="javascript"--><div class="source"><pre><br><!DOCTYPE html <span class="cpp-keyword">PUBLIC</span> <span class="cpp-literal">"-//W3C//DTD XHTML [[[[[1]]]]].[[[[[0]]]]] Strict//EN"</span> <span class="cpp-literal">"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"</span>><br><html xmlns=<span class="cpp-literal">"http://www.w3.org/[[[[[1999]]]]]/xhtml"</span> xml:lang=<span class="cpp-literal">"en"</span> lang=<span class="cpp-literal">"en"</span>><br><head><br> <meta http-equiv=<span class="cpp-literal">"Content-Type"</span> content=<span class="cpp-literal">"text/html; charset=UTF-[[[[[8]]]]]"</span> /><br> <link rel=<span class="cpp-literal">"stylesheet"</span> href=<span class="cpp-literal">"style.css"</span> /><br> <title>Testing</title><br> <br> <script type=<span class="cpp-literal">"text/javascript"</span>><br> <span class="cpp-comment">// <![CDATA[</span><br> <span class="cpp-keyword">var</span> agent = navigator.userAgent.toLowerCase();<br> <span class="cpp-keyword">var</span> is_ie = ((agent.indexOf(<span class="cpp-literal">"msie"</span>)!=-[[[[[<span class="cpp-number">1</span>]]]]]) && (agent.indexOf(<span class="cpp-literal">"opera"</span>)==-[[[[[<span class="cpp-number">1</span>]]]]]));<br> <br> <span class="cpp-keyword">function</span> initpage() {<br> document.forms[[[[[[<span class="cpp-number">0</span>]]]]]].elements[[[[[[<span class="cpp-number">0</span>]]]]]].focus();<br> alert(agent);<br> }<br> <span class="cpp-comment">// ]]></span><br> </script><br></head><br><body onload=<span class="cpp-literal">"javascript:initpage()"</span>><br><br><form id=<span class="cpp-literal">"sendmessage"</span> action=<span class="cpp-literal">"listen.pl"</span> method=<span class="cpp-literal">"get"</span>><br> <textarea name=<span class="cpp-literal">"message"</span> rows=<span class="cpp-literal">"[[[[[20]]]]]"</span> cols=<span class="cpp-literal">"[[[[[40]]]]]"</span>></textarea><br> <br /><br> <input type=<span class="cpp-literal">"submit"</span> value=<span class="cpp-literal">"Send"</span>/><br></form><br><br><br><br></body><br></html><br><br><br><br><br><br></pre></div><!--ENDSCRIPT-->
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement