admin管理员组

文章数量:1334887

I have the folowing html. It passes the w3 validator, but my javascript alert does not work. Can anyone see any problems or have any suggestions on how to get it to work?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ".dtd">
<html xmlns="">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Company Name™</title>
        <link rel="stylesheet" type="text/css" href="css/style.css" />
        <style type="text/css">
            BODY {
                background-image: url(images/bg4.jpg); 
                background-repeat:no-repeat; 
                background-position:center top;
                background-attachment: fixed; 
            }
        </style>
        <script type="text/javascript">
            alert("HELLO");
        </script>
    </head>
    <body>
        <div id="wrapper">
            <div id="header">
                <div class="logo">
                    <img src="images/fcflogo.png" width="295" height="100" align="left" alt="logo"/>
                </div>
                <div class="header1">
                    <a href="financialprotection.html">100% Financial Protection</a>
                    <hr/>
                </div>
                <div class="nav">Home| Flights| Hotels| Villas | CarHire| Attractions| Contact</div>
            </div>
            <div class="ver1">
                <h2>Can't find what your looking for?</h2>
            </div>
            <div class="enq1">
                <h2>Enquiry Form</h2>
            </div>
            <div class="hor1">
                <h2>Our Service</h2>
                <a>Company Name are one of the leading independent travel panies specialising in Florida Holidays. We have a wide range of major Charter and Scheduled airlines to choose from as well as over 10,000 Hotel and Villa deals. Our aim is to provide you the customer with a truly fantastic vacation in Florida from start to finish at affordable prices. We are not mitted to any airline or Tour operator so are totally mitted to finding you the best deal.</a>
            </div>
            <div class="hor1"><a>FLIGHTS</a></div>
            <div class="ver2"><a>HOTELS</a></div>
            <div class="ver2"><a>VILLAS</a></div>
            <div class="hor1"><a>CAR HIRE</a></div>
            <div class="hor1"><a>ATTRACTIONS</a></div>
        </div>
        <hr/>
        <div id="footer"><a>FOOTER</a></div>
    </body>
</html>

EDIT - To all below, I do have javascript turned on, I am using debian with firefox, noScript is disabled, but the alert does not appear, even if I move it to the body.

I have the folowing html. It passes the w3 validator, but my javascript alert does not work. Can anyone see any problems or have any suggestions on how to get it to work?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Company Name™</title>
        <link rel="stylesheet" type="text/css" href="css/style.css" />
        <style type="text/css">
            BODY {
                background-image: url(images/bg4.jpg); 
                background-repeat:no-repeat; 
                background-position:center top;
                background-attachment: fixed; 
            }
        </style>
        <script type="text/javascript">
            alert("HELLO");
        </script>
    </head>
    <body>
        <div id="wrapper">
            <div id="header">
                <div class="logo">
                    <img src="images/fcflogo.png" width="295" height="100" align="left" alt="logo"/>
                </div>
                <div class="header1">
                    <a href="financialprotection.html">100% Financial Protection</a>
                    <hr/>
                </div>
                <div class="nav">Home| Flights| Hotels| Villas | CarHire| Attractions| Contact</div>
            </div>
            <div class="ver1">
                <h2>Can't find what your looking for?</h2>
            </div>
            <div class="enq1">
                <h2>Enquiry Form</h2>
            </div>
            <div class="hor1">
                <h2>Our Service</h2>
                <a>Company Name are one of the leading independent travel panies specialising in Florida Holidays. We have a wide range of major Charter and Scheduled airlines to choose from as well as over 10,000 Hotel and Villa deals. Our aim is to provide you the customer with a truly fantastic vacation in Florida from start to finish at affordable prices. We are not mitted to any airline or Tour operator so are totally mitted to finding you the best deal.</a>
            </div>
            <div class="hor1"><a>FLIGHTS</a></div>
            <div class="ver2"><a>HOTELS</a></div>
            <div class="ver2"><a>VILLAS</a></div>
            <div class="hor1"><a>CAR HIRE</a></div>
            <div class="hor1"><a>ATTRACTIONS</a></div>
        </div>
        <hr/>
        <div id="footer"><a>FOOTER</a></div>
    </body>
</html>

EDIT - To all below, I do have javascript turned on, I am using debian with firefox, noScript is disabled, but the alert does not appear, even if I move it to the body.

Share Improve this question edited Apr 8, 2014 at 17:40 Greg 2,1731 gold badge22 silver badges23 bronze badges asked Sep 19, 2010 at 13:39 Jim JeffriesJim Jeffries 10.1k16 gold badges65 silver badges105 bronze badges 4
  • It works for me on Windows 7 with Google Chrome 7. What browser, browser version and operating system are you using? Have you tested this on a different browser? – Rupert Madden-Abbott Commented Sep 19, 2010 at 13:46
  • 1 If you're talking about the alert("HELLO"), that works just fine. What are you expecting it to do that it's not doing? – Robusto Commented Sep 19, 2010 at 13:48
  • the alert does not appear for me. I am using Linux (debian) and firefox. I have also tested in chrome and it doesnt work. – Jim Jeffries Commented Sep 19, 2010 at 13:57
  • 1 Does this work: virkkunen/b/this-will-surely-work.html ? – Matti Virkkunen Commented Sep 19, 2010 at 14:05
Add a ment  | 

5 Answers 5

Reset to default 4

First, take out the trade mark. That extended characters is probably killing the closing </title> tag. I"m pretty sure that's your issue.

Failing that, remove every element above the <script> tag and see if it executes. If it does, restore each element one-by-one. Something above the script tag is preventing it from being parsed.

For me, your code works, the alert appears. Have you enabled javascript in your browser?

Works fine on FF 3 and IE 8 for me.

Have you disabled JavaScript on your browser by any chance?

The script is never being invoked and that's why it is not alerting anything. Either move that script tag from within head to within body tag of the document. Or enclose it within a function and invoke it from onload attribute of body tag.

You might want to try clearing your cache to make sure that your browser is loading the file with the alert in it instead of an older, cached copy. If that doesn't work, I'd suggest trying it with Firefox/Firebug and checking for errors in the javascript console. If you're loading the file with AJAX, you'll need to move the script tag to the body of the document. Most of the time AJAX libraries will ignore the HEAD element and only include elements within the BODY tag.

本文标签: htmlWhy does my javascript not workStack Overflow