[web] quickie, javascript and xhtml

Started by
8 comments, last by kanzler 19 years, 2 months ago
  <script type="text/&#106avascript">
  // <![CDATA[
    function initpage() {
      document.forms[0][0].focus();
    }
  // ]]>
  </script>
My xhtml validates as strict. I'm using <body onload="&#106avascript:initpage()"&gt;, nothing happens In fact, anything with &#111;nX="&#106avascript:initpage()" doesn't work, although &#106avascript:alert('abc') will If I rename the file to .html, it works! Yes, I know I can put &#111;nX="document.forms[0][0].focus()" directly, but that isn't the point. I'm using Firefox Thanks already
spraff.net: don't laugh, I'm still just starting...
Advertisement
Use
<body onload="initpage()">


EDIT: I see that's not the problem here. Sorry.
Tried the web dev forum?

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

You can't have multi-dimensional forms on a HTML page. Try this:

  <script type="text/&#106avascript">  // <![CDATA[    function initpage() {      document.forms[0].elements[0].focus();    }  // ]]>  </script>



EDIT: &#106avascript does not even support multi-dimensional arrays.
I would recomend against putting &#106avascript inside html files. Instead, put it in a .js file and include that. To get your initialize event, do
window.onload = initialize;function initialize(){  //Stuff}


I recomend Quirksmode for all &#106avascript and CSS stuff.
Neither of those things help. Thanks anyway
spraff.net: don't laugh, I'm still just starting...
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)
function go() { alert('x'); }

and in the page
<button onmouseover="&#106avascript:go()" />

and that won't do anything, but if I change it to &#106avascript:alert directly, it does work.
spraff.net: don't laugh, I'm still just starting...
First of all, you can leave out the &#106avascript: protocol for onX handlers because those are targeted specifically at &#106avascript.<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/&#106avascript">  // <![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="&#106avascript:initpage()"&gt;<br><br>&lt;div id="oldfix"&gt;<br>&lt;div id="central"&gt;<br>&lt;form id="sendmessage" action="listen.pl" method="get"&gt;<p><br>  &lt;textarea name="message"  rows="20" cols="40"&gt;&lt;/textarea&gt;<br>  &lt;br /&gt;<br>  &lt;input type="submit" value="Send"/&gt;<br></p>&lt;/form&gt;<br></div><br></div><br><br>&lt;/body&gt;<br>&lt;/html&gt;</pre>
spraff.net: don't laugh, I'm still just starting...
&#106avascript 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 &#106avascript development, it has an excellent &#106avascript debugger. <br><br>The code below works in IE and Mozilla.<br><br><!--STARTSCRIPT--><!--source lang="javascript"--><div class="source"><pre><br>&lt;!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>&gt;<br>&lt;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>&gt;<br>&lt;head&gt;<br> &lt;meta http-equiv=<span class="cpp-literal">"Content-Type"</span> content=<span class="cpp-literal">"text/html; charset=UTF-[[[[[8]]]]]"</span> /&gt;<br> &lt;link rel=<span class="cpp-literal">"stylesheet"</span> href=<span class="cpp-literal">"style.css"</span> /&gt;<br> &lt;title&gt;Testing&lt;/title&gt;<br> <br> &lt;script type=<span class="cpp-literal">"text/javascript"</span>&gt;<br> <span class="cpp-comment">// &lt;![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>]]]]]) &amp;&amp; (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">// ]]&gt;</span><br> &lt;/script&gt;<br>&lt;/head&gt;<br>&lt;body onload=<span class="cpp-literal">"javascript:initpage()"</span>&gt;<br><br>&lt;form id=<span class="cpp-literal">"sendmessage"</span> action=<span class="cpp-literal">"listen.pl"</span> method=<span class="cpp-literal">"get"</span>&gt;<br> &lt;textarea name=<span class="cpp-literal">"message"</span> rows=<span class="cpp-literal">"[[[[[20]]]]]"</span> cols=<span class="cpp-literal">"[[[[[40]]]]]"</span>&gt;&lt;/textarea&gt;<br> &lt;br /&gt;<br> &lt;input type=<span class="cpp-literal">"submit"</span> value=<span class="cpp-literal">"Send"</span>/&gt;<br>&lt;/form&gt;<br><br><br><br>&lt;/body&gt;<br>&lt;/html&gt;<br><br><br><br><br><br></pre></div><!--ENDSCRIPT-->

This topic is closed to new replies.

Advertisement