Javascript将无法执行

时间:2013-01-06 17:00:51

标签: javascript html

我正在开发一个新网站。我之前编写了很多编码,但没有像这个先进。 我有一个html文件,这是主页面。现在,我正在通过ajax更改页面的内容。一切正常。因此,当我点击“主页”菜单时,它会调用该函数,并获取其中包含内容的另一个HTML文件。

在我的主要HTML中,我有这个:

<html>
    <head>
    <title>
        Estancia
    </title>
    <link rel="stylesheet" href="css/stylesheet.css"/>
    <link rel="stylesheet" href="webfont/webFontStylesheet.css" type="text/css" charset="utf-8"/>
    <script type="text/javascript" src="js/ajax.js"></script>
</head>
<body onLoad = "javascript:home()">
    <div id = "bodyLeft">
        <!-- This is just a sideskirt -->
    </div>
    <div id = "bodyRight">
        <!-- This is just a sideskirt -->
    </div>

    <div id = "header">
        <h1>
            Estancia
        </h1>

    </div>
    <div id = "footer">
        <h5>
            Copyright&copy; 2013
        </h5>
    </div>
    <div id = "background">
        <!-- This is just the background color -->
    </div>
    <div id = "scrollCover">
        <!-- Used to cover the scrolled content -->
    </div>
    <div id = "body">

    </div>
    <div id = "content">
        If you see this then the page did not load correctly. Try reloading the page. If problem persists, try reinstalling the Mozilla Firefox Browser. <br/><br/> Error: Javascript may not be working. <br/> Fix: Enable Javascript in your browser.
    </div>
    <div id = "menu">
        <div id = "home" onClick="javascript:home()">
            <a href = "#"> Home </a>
        </div>
        <div id = "information">
            <a href = "#"> Information </a>
            <span id = "aboutDrop">
                <a class = "about" href="#" onClick = "javascript:about()"> About </a>
                <br/>
                <a class = "map" href="#" onClick = "javascript:map()"> Map </a>
            </span>
        </div>
        <div id = "history" onClick = "javascript:history()">
            <a href = "#"> History </a>
        </div>
        <div id = "pictureGallary">
            <a href = "#"> Picture Gallary </a>
            <span id = "pictureGalleryDrop">
                <a class = "ef" href = "#" onClick = "javascript:estanciaFarm()"> Estancia Farm </a>
                <br/>
                <a class = "ca" href = "#" onClick = "javascript:cattle()"> Cattle </a>
                <br/>
                <a class = "ga"href = "#" onClick = "javascript:game()"> Game </a>
                <br/>
                <a class = "go" href = "#" onClick = "javascript:goats()"> Goats </a>
                <br/>
                <a class = "sh" href = "#" onClick = "javascript:sheep()"> Sheep </a>
            </span>
        </div>
    </div>
</body>

此处没有问题...当我点击它正确加载时。 因此,当单击Home时,它会通过ajax更改内容,如下所示:

var xmlhttp = new XMLHttpRequest();
    var path;
    var div;

function home()
{
    path = "html/home.html";
    div = "content";
    AJAX(path, div);
}

    function AJAX(path, div)
{
    xmlhttp.open("GET", path, false);
    xmlhttp.send();
    if (xmlhttp.readyState == 4)
    {
        document.getElementById(div).innerHTML = xmlhttp.responseText;
    }
}

正如您所看到的,它会将另一个html文件加载到内容中。 所以,这是html文件的代码:

<html>
<head>
    <link rel="stylesheet" href="css/homeMenu.css"/>
    <script type="text/javascript" src="js/javascript.js"></script>
</head>
<body>
    <div id = "slideshow">
        <div id = "first">
            <img id = "a1" src = "images/home/img1_e.jpg"/>
            <img id = "a2" src = "images/home/img2_e.jpg"/>
            <img id = "a3" src = "images/home/img3_e.jpg"/>
            <img id = "a4" src = "images/home/img4_e.jpg"/>
        </div>
        <div id = "second">
            <img id = "b4" src = "images/home/img4_e.jpg"/>
            <img id = "b1" src = "images/home/img1_e.jpg"/>
            <img id = "b2" src = "images/home/img2_e.jpg"/>
            <img id = "b3" src = "images/home/img3_e.jpg"/>
        </div>
        <div id = "third">
            <img id = "c3" src = "images/home/img3_e.jpg"/>
            <img id = "c4" src = "images/home/img4_e.jpg"/>
            <img id = "c1" src = "images/home/img1_e.jpg"/>
            <img id = "c2" src = "images/home/img2_e.jpg"/>

        </div>
        <div id = "fourth">
            <img id = "d2" src = "images/home/img2_e.jpg"/>
            <img id = "d3" src = "images/home/img3_e.jpg"/>
            <img id = "d4" src = "images/home/img4_e.jpg"/>
            <img id = "d1" src = "images/home/img1_e.jpg"/>
        </div>
    </div>
    </body>

当我自己加载这个html文件时,它工作正常。我需要改变的是在每个图像路径的开头添加“../”。我相信你会知道为什么。快速描述是,如果它自己,如果在另一个文件中,所以它需要返回一次。当它通过ajax加载到另一个主要的html中时,它在每个文件外部并且只是进入它。好的,这很清楚。

问题......我面临的唯一问题是当我自己加载home.html文件时,它会像一块蛋糕一样执行它的javascript。

但是,当它加载到主html中时,它就不起作用了。没有办法执行它。我已经尝试过内部javascript,但它还没有用。

这是javascript:

var useBSNns;

        if (useBSNns)
        {
            if (typeof(bsn) == "undefined")
                bsn = {}
            var _bsn = bsn;
        }
        else
        {
            var _bsn = this;
        }

        _bsn.Crossfader = function (divs, fadetime, delay )
        {   
            this.nAct = -1;
            this.aDivs = divs;

            for (var i=0;i<divs.length;i++)
            {
                document.getElementById(divs[i]).style.opacity = 0;
                document.getElementById(divs[i]).style.position = "absolute";
                document.getElementById(divs[i]).style.filter = "alpha(opacity=0)";
                document.getElementById(divs[i]).style.visibility = "hidden";
            }

            this.nDur = fadetime;
            this.nDelay = delay;

            this._newfade();
        }

        _bsn.Crossfader.prototype._newfade = function()
        {
            if (this.nID1)
                clearInterval(this.nID1);

            this.nOldAct = this.nAct;
            this.nAct++;
            if (!this.aDivs[this.nAct]) this.nAct = 0;

            if (this.nAct == this.nOldAct)
                return false;

            document.getElementById( this.aDivs[this.nAct] ).style.visibility = "visible";

            this.nInt = 50;
            this.nTime = 0;

            var p=this;
            this.nID2 = setInterval(function() { p._fade() }, this.nInt);
        }

        _bsn.Crossfader.prototype._fade = function()
        {
            this.nTime += this.nInt;

            var ieop = Math.round( this._easeInOut(this.nTime, 0, 1, this.nDur) * 100 );
            var op = ieop / 100;
            document.getElementById( this.aDivs[this.nAct] ).style.opacity = op;
            document.getElementById( this.aDivs[this.nAct] ).style.filter = "alpha(opacity="+ieop+")";

            if (this.nOldAct > -1)
            {
                document.getElementById( this.aDivs[this.nOldAct] ).style.opacity = 1 - op;
                document.getElementById( this.aDivs[this.nOldAct] ).style.filter = "alpha(opacity="+(100 - ieop)+")";
            }

            if (this.nTime == this.nDur)
            {
                clearInterval( this.nID2 );

                if (this.nOldAct > -1)
                    document.getElementById( this.aDivs[this.nOldAct] ).style.visibility = "hidden";    

                var p=this;
                this.nID1 = setInterval(function() { p._newfade() }, this.nDelay);
            }
        }

        _bsn.Crossfader.prototype._easeInOut = function(t,b,c,d)
        {
            return c/2 * (1 - Math.cos(Math.PI*t/d)) + b;
        }

请问,当home.html将其加载到主html中时,它不想执行,但是当home.html自行执行时它有效吗?

1 个答案:

答案 0 :(得分:0)

我知道你已经投入了大量的工作,但是查看一个jQuery插件(例如Cycle)可能会有所帮助。 http://malsup.com/jquery/cycle


另外,您需要从正在检索的<script type="text/javascript" src="js/javascript.js"></script>页面中删除Home.html,并将其放在要加载辅助页面的main.html文件的头部。它将被调用不是通过AJAX加载内联执行,这是你遇到的问题。我们将其移至main.html,我们会在加载home.html页面时手动调用它。

我已经改变了你的AJAX方法,以便在AJAX成功时采用回调方法或函数。您会看到AJAX方法的第三个参数是完成后调用的函数,在我们的示例中,startBSN您将看到我使用此方法进一步向下调整js/javascript函数的位置

var xmlhttp = new XMLHttpRequest();
var path;
var div;

function home()
{
   path = "html/home.html";
   div = "content";
   AJAX(path, div, 'startBSN');
}

function AJAX(path, div, callback)
{
    xmlhttp.open("GET", path, false);
    xmlhttp.send();
    if (xmlhttp.readyState == 4)
    {
       document.getElementById(div).innerHTML = xmlhttp.responseText;
       window[callback]();
    }
}

我已移除您的js/javascript.js并将其添加到main.html的标题中,您应将其从home.html

中删除

我还将js/javascript.js代码包装到一个名为startBSN的可调用函数中,否则它将作为匿名函数在页面加载时运行。

以下是包含周围函数js.javascript

startBSN文件
function startBSN(){
    var useBSNns;

    if (useBSNns)
    {
        if (typeof(bsn) == "undefined")
            bsn = {}
        var _bsn = bsn;
    }
    else
    {
        var _bsn = this;
    }


    _bsn.Crossfader = function (divs, fadetime, delay )
    {   
        this.nAct = -1;
        this.aDivs = divs;

        for (var i=0;i<divs.length;i++)
        {
            document.getElementById(divs[i]).style.opacity = 0;
            document.getElementById(divs[i]).style.position = "absolute";
            document.getElementById(divs[i]).style.filter = "alpha(opacity=0)";
            document.getElementById(divs[i]).style.visibility = "hidden";
        }

        this.nDur = fadetime;
        this.nDelay = delay;

        this._newfade();
    }

    _bsn.Crossfader.prototype._newfade = function()
    {
        if (this.nID1)
            clearInterval(this.nID1);

        this.nOldAct = this.nAct;
        this.nAct++;
        if (!this.aDivs[this.nAct]) this.nAct = 0;

        if (this.nAct == this.nOldAct)
            return false;

        document.getElementById( this.aDivs[this.nAct] ).style.visibility = "visible";

        this.nInt = 50;
        this.nTime = 0;

        var p=this;
        this.nID2 = setInterval(function() { p._fade() }, this.nInt);
    }

    _bsn.Crossfader.prototype._fade = function()
    {
        this.nTime += this.nInt;

        var ieop = Math.round( this._easeInOut(this.nTime, 0, 1, this.nDur) * 100 );
        var op = ieop / 100;
        document.getElementById( this.aDivs[this.nAct] ).style.opacity = op;
        document.getElementById( this.aDivs[this.nAct] ).style.filter = "alpha(opacity="+ieop+")";

        if (this.nOldAct > -1)
        {
            document.getElementById( this.aDivs[this.nOldAct] ).style.opacity = 1 - op;
            document.getElementById( this.aDivs[this.nOldAct] ).style.filter = "alpha(opacity="+(100 - ieop)+")";
        }

        if (this.nTime == this.nDur)
        {
            clearInterval( this.nID2 );

            if (this.nOldAct > -1)
                document.getElementById( this.aDivs[this.nOldAct] ).style.visibility = "hidden";    

            var p=this;
            this.nID1 = setInterval(function() { p._newfade() }, this.nDelay);
        }
    }

    _bsn.Crossfader.prototype._easeInOut = function(t,b,c,d)
    {
        return c/2 * (1 - Math.cos(Math.PI*t/d)) + b;
    }
}