简体   繁体   中英

Why does my javascript not work

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.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/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 companies 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 committed to any airline or Tour operator so are totally committed 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM