Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
外部javascript文件中的错误_Javascript_Html - Fatal编程技术网

外部javascript文件中的错误

外部javascript文件中的错误,javascript,html,Javascript,Html,我有一个javascript,当它放在html的头部时就可以工作。 它会动态更改div“bg”中的背景图像 <script type="text/javascript">window.onload = function () { var header = document.getElementById('bg'); var pictures = new Array('http://someurl.com/bgs/1.jpg',

我有一个javascript,当它放在html的头部时就可以工作。 它会动态更改div“bg”中的背景图像

<script type="text/javascript">window.onload = function () {
    var header = document.getElementById('bg');
    var pictures = new Array('http://someurl.com/bgs/1.jpg',
                             'http://someurl.com/bgs/2.jpg',
                             'http://someurl.com/bgs/3.jpg',
                             'http://someurl.com/bgs/4.jpg',
                             'http://someurl.com/bgs/5.jpg');
    var numPics = pictures.length;
    if (document.images) {
        var chosenPic = Math.floor((Math.random() * numPics));
        header.style.backgroundImage = 'url(' + pictures[chosenPic] + ')';</script>
我也犯了同样的错误。 有人能帮我把脚本作为一个外部的.js文件使用吗?

当你附加}时,你正在关闭用if document.images{打开的块

这仍然使函数保持打开状态


你需要另一个}。

这不是一个评论吗?我的意思是,这基本上是一个简单的错误修复?试着更彻底,老兄!
window.onload = function () {
    var header = document.getElementById('bg');
    var pictures = new Array('http://someurl.com/bgs/1.jpg',
                             'http://someurl.com/bgs/2.jpg',
                             'http://someurl.com/bgs/3.jpg',
                             'http://someurl.com/bgs/4.jpg',
                             'http://someurl.com/bgs/5.jpg');
    var numPics = pictures.length;
    if (document.images) {
        var chosenPic = Math.floor((Math.random() * numPics));
        header.style.backgroundImage = 'url(' + pictures[chosenPic] + ')';
    }