0

For a class project i am trying to count the number of clicks a button gets, save the search term, and then open a new html file. my problem is that the "doCoolThings" function will not call the "newWindow" function. I cannot figure out how to get the newWindow function to execute after i click submit.

here is my code:

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
  • What, if any, output do you see in your console when attempting to run this? A very basic representation of this setup works as expected: jsbin.com/izeyej/3/edit Commented Apr 23, 2012 at 4:17
  • okay change window.onload = init; to window.onload = init(); and check Commented Apr 23, 2012 at 4:19
  • i get the alert about the test count, but not a new window/i changed it to window.location to open in the same window. I can tell the doCoolThings function works because i use the results in a different window Commented Apr 23, 2012 at 4:20
  • window.onload = init(); made it worse Commented Apr 23, 2012 at 4:22

1 Answer 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.