0

对于一个类项目,我试图计算按钮获得的点击次数,保存搜索词,然后打开一个新的 html 文件。我的问题是“doCoolThings”函数不会调用“newWindow”函数。单击提交后,我无法弄清楚如何执行 newWindow 函数。

这是我的代码:

window.onload = init;
function init() {
    var myButton = document.getElementById("submitButton");
    myButton.onclick = doCoolThings;
    }


function doCoolThings(){
    alert("test count");
    localStorage.searchTerms = document.getElementById("searchWord").value;
    //var searchOutput = document.getElementById("searchOutput");
    searchWord.innerHTML = searchTerms;

    if (localStorage.clickcount)
        {
        localStorage.clickcount=Number(localStorage.clickcount)+1;
        //window.open("scroogleresults.html");
        }
    else
        {
        localStorage.clickcount=1;
        //window.location="scroogleresults.html";       
        }
    var myCounter = document.getElementById("numberClicks");
    var counter = localStorage.clickcount;
    myCounter.innerHTML = counter;
    newWindow();
}
function newWindow(){       
        alert("new window");
        window.open("scroogleresults.html");            
    }
4

1 回答 1

1

If this is the extent of your code, it looks to me like you have some javascript errors. You should be checking the error console or debug console in your browser for javascript errors that cause your scripts to abort. Here are a couple errors I see:

Error #1

In doCoolThings(), when you do this:

searchWord.innerHTML = searchTerms;

I don't see any place that searchTerms is defined so this would generate a script error.

Error #2

You should be using getItem() and setItem() to access localStorage.

于 2012-04-23T04:12:43.440 回答