admin管理员组

文章数量:1387388

I'm encountering a problem where my body onload="constructor()" is not being run. It works fine for me in firefox but I don't understand why it's not working for me in chrome. Here's the code I'm working with, I made a separate file and deleted everything to the bare minium to try figure out what was going wrong:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8" />
    <title>Personality Font</title>
    <link rel="stylesheet" type="text/css" href="p1.css" />
    <script type="text/javascript" src="data.js"></script>
    <script type="text/javascript">
    //<![CDATA[


        function constructor(which)
        {
            console.log("IN CONSTRUCTOR"); //In Constructor
            var text = document.createElement('p');
            text.appendChild(document.createTextNode("BLAH"));
            document.getElementsByTagName('body')[0].appendChild(text);

        }

    //]]>
    </script>
</head>

<body onload = "constructor();">
    <h1>Personal Fonts: Find the Typeface that Matches Your Personality</h1>
    <form>

    </form>


</body>
</html>

I'm encountering a problem where my body onload="constructor()" is not being run. It works fine for me in firefox but I don't understand why it's not working for me in chrome. Here's the code I'm working with, I made a separate file and deleted everything to the bare minium to try figure out what was going wrong:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8" />
    <title>Personality Font</title>
    <link rel="stylesheet" type="text/css" href="p1.css" />
    <script type="text/javascript" src="data.js"></script>
    <script type="text/javascript">
    //<![CDATA[


        function constructor(which)
        {
            console.log("IN CONSTRUCTOR"); //In Constructor
            var text = document.createElement('p');
            text.appendChild(document.createTextNode("BLAH"));
            document.getElementsByTagName('body')[0].appendChild(text);

        }

    //]]>
    </script>
</head>

<body onload = "constructor();">
    <h1>Personal Fonts: Find the Typeface that Matches Your Personality</h1>
    <form>

    </form>


</body>
</html>
Share Improve this question asked Nov 11, 2012 at 22:01 baritbarit 1133 silver badges10 bronze badges 4
  • @barit Where is the parameter which in call? – SaidbakR Commented Nov 11, 2012 at 22:04
  • @sємsєм It's not used anyways. – John Dvorak Commented Nov 11, 2012 at 22:06
  • @sємsєм I'm not passing anything in right now with the way the code is written but thanks to Quentin down below I realized my problem was calling my function constructor. – barit Commented Nov 11, 2012 at 22:11
  • @barit I did not voted down the question at all! In contrast, I voted it up! – SaidbakR Commented Nov 12, 2012 at 5:33
Add a ment  | 

1 Answer 1

Reset to default 8

Chrome has a built-in function with the name constructor. Call the function something else.

本文标签: javascriptWhy is body onload not working in chromeStack Overflow