Question

I have

<!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>
    <title>Function test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
    <script type="text/javascript">
        function doFunction(towindow)
        {
            towindow.value='2';
        }   
    </script>
</head>
<body>
    <div>
        <textarea id="outputarea" rows="6" cols="60"></textarea> <br />
        <button type="button" onclick="doFunction(outputarea)">doFunction </button>
    </div>
</body>
</html>

This does not work. When the button is clicked, then nothing happens.

If I remove the <!DOCTYPE .... then everything works fine.

I would like to keep the DOCTYPE for validation and I have found this exact declaration from the W3schools website. The code does validate. I am using Firefox 10.0. When I use Chrome 27.0.1453.116 the problem does not occur. Also, the problem does not occur when I use Explorer 10.

It seems like there is a problem between the JavaScript, the DOCTYPE declaration, and Firefox.

What is the problem? How might I fix this?

(I see other questions (see for example this-1, this-2, this-3, this-4) with some of the same question, but they didn't help)

Was it helpful?

Solution

The problem is not the DOCTYPE; it may be that Firefox 10 (which is pretty old now BTW) is interpreting the code differently depending on the DOCTYPE, but the real problem is your Javascript.

Your code onclick="doFunction(outputarea)" is not really correct; some browsers will guess that you want the element with the ID outputarea but that's not standard behavior. Your should do something like this instead:

<script type="text/javascript">
    function doFunction(towindowId)
    {
        var towindow = document.getElementById(towindowId);
        towindow.value='2';
    }   
</script>
...
<button type="button" onclick="doFunction('outputarea')">doFunction </button>

Also, you should always check for Javascript errors in Firefox's error console - I tested your original code in Firefox 10 and got the error "Error: toWindowId is not defined".

FYI, XHTML (strict mode or otherwise) is rarely needed; you might want to consider just the regular HTML5 doctype, <!DOCTYPE html>, unless you have a specific reason for needing XHTML. See this article: http://www.webdevout.net/articles/beware-of-xhtml. Also note that the W3Schools info is often not up-to-date with current trends and practices.

OTHER TIPS

A doctype is required for all new web pages. You aren't serving your pages as XHTML so using the doctype you have now makes no sense. Use the new one because it puts all browsers back to IE6 in standards mode where you want to be and it's shorter and easier to remember. <!DOCTYPE html>

Firefox 10 is an antique and I don't know why you think anyone uses it. Ignore it. The current version is 21.

So just changing your first two lines to this and it works in the current version of Chrome and Firefox:

<!DOCTYPE html>
<html>

Didn't look at IE.

Also, Remove the meta element. That's only for people who are viewing your page saved to the desktop and not over the internet (unless you need that). Also, it's no longer necessary for the type attribute for the script tag.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top