Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

322
Views
Function onclick not working for multiple windows

I am making a simple javascript clicker game. Both the document.onclick and the clicksWin.onclick work fine but for some reason the multiWin.onclick does nothing.

Here is my code:

var clicksWin = window.open("","_blank", "width=200 height=150");
clicksWin.document.write("<!DOCTYPE html><head></head><body><p>Clicks: 0</p><p>Click Multi: 1<p><p>Cost: 10</p></body>");

var clicks=0;
var clickMulti=1;
var cost=10;
var costCurve=1.5;
var multiMulti=1;
var multiMultiUnlocked=0;
var mmCost=1000;
var mmCostCurve=1.5;



document.onclick=function(event){ 
    if(event===undefined){ event= window.event; }
    
    clicks = clicks + clickMulti;

    if(clicks >= 1000 && multiMultiUnlocked==0) {
        var multiWin = window.open("","_blank", "width=200 height=75 top=170");
        multiWin.document.write("<!DOCTYPE html><head></head><body><p>Multi Multi: " + multiMulti + "</p><p>Cost: " + mmCost + "</p></body>");
        multiMultiUnlocked = 1;

    }

    clicksWin.document.body.innerHTML="<p>Clicks: " + Math.ceil(clicks) + "</p><p>Click Multi: " + clickMulti + "</p><p>Cost: " + Math.ceil(cost) + "</p>"; 

};

clicksWin.onclick=function(event){
    if(event===undefined){ event= window.event; }
    if(clicks >= cost){
        clickMulti = clickMulti + multiMulti;
        clicks = clicks - Math.ceil(cost);
        cost = cost * costCurve;
        costCurve = costCurve * 1.03;
        
        clicksWin.document.body.innerHTML="<p>Clicks: " + Math.ceil(clicks) + "</p><p>Click Multi: " + clickMulti + "</p><p>Cost: " + Math.ceil(cost) + "</p>"; 
    } 
};

multiWin.onclick=function(event){
    if(event===undefined){ event= window.event; }
    
    if(clicks >= mmCost){
        multiMulti = multiMulti + 0.1;
        clicks = clicks - Math.ceil(mmCost);
        mmCost = mmCost * mmCostCurve;
        mmCostCurve = mmCostCurve * 1.03;
        multiWin.document.body.innerHTML="<p>Multi Multi: " + multiMulti + "</p><p>Cost: " + Math.ceil(mmCost) + "</p>";
    }
    
};

void(0);

P.S. For some reason in this code snippet it says there is an error but when I run it via a bookmarklet it works fine

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You're declaring multiWin inside the onclick listener for document:

var multiWin = window.open...

But you're immediately using multiWin:

multiWin.onclick=function(...

I.e. your multiWin is declared only when you click, but you're using it before that.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!