Jump to content
  • Advertisement
Sign in to follow this  
walkingcarcass

[web] quickie, javascript and xhtml

This topic is 4998 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

  <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

Share this post


Link to post
Share on other sites
Advertisement
You can't have multi-dimensional forms on a HTML page. Try this:

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



EDIT: javascript does not even support multi-dimensional arrays.

Share this post


Link to post
Share on other sites
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

window.onload = initialize;

function initialize()
{
//Stuff
}


I recomend Quirksmode for all javascript and CSS stuff.

Share this post


Link to post
Share on other sites
Try giving your form and element an id tag and reference them like document.forms['form_id'].elements['element_id'].

Share this post


Link to post
Share on other sites
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="javascript:go()" />

and that won't do anything, but if I change it to javascript:alert directly, it does work.

Share this post


Link to post
Share on other sites
First of all, you can leave out the javascript: protocol for onX handlers because those are targeted specifically at javascript.

Secondly, do you have a link to the complete page?

Share this post


Link to post
Share on other sites
<!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()">

<div id="oldfix">
<div id="central">
<form id="sendmessage" action="listen.pl" method="get">


<textarea name="message" rows="20" cols="40"></textarea>
<br />
<input type="submit" value="Send"/>

</form>



</body>
</html>

Share this post


Link to post
Share on other sites
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.

The code below works in IE and Mozilla.


<!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()">

<form id="sendmessage" action="listen.pl" method="get">
<textarea name="message" rows="[[[[[20]]]]]" cols="[[[[[40]]]]]"></textarea>
<br />
<input type="submit" value="Send"/>
</form>



</body>
</html>





Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!