views:

36

answers:

1

Hi,

I have two jsp(one is main page, another one is new window) and single javascript file.

MainPage and New Window have 'Test' , 'ClickedCount' buttons.

First, I click 3 times in MainPage 'Test' button . Then i open New Window.
Now , i click 2 times in newWindow 'Test' button.
So totally i clicked 5 times.

each time i click, i count the button click in javascript.

But mainwindow shows only 3 times, --> getTestButtonClickedCount()
And new window shows only 2 times . --> getTestButtonClickedCount()

Here i use same single javascript file (count.js)

So how to get total clickCount both window (mainWindow + NewWindow) 'Test' button.

MainWindow.jsp

<html>
     <head>
          <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
          <script src="count.js" type="text/javascript"></script>
     </head>
<body>
 <input type="button" value="Test" onclick="countTestButtonClick()"/>
 <input type="button" value="ClickedCount" onclick="getTestButtonClickedCount();"/>

 <a href="#" onclick = "window.open('NewWindow.jsp','newwindow',
                        'width=400,height=200')">Click to open New window</a>
</body>
</html>

NewWindow.jsp

<html>
     <head>
           <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
           <script src="count.js" type="text/javascript"></script>
     </head>
<body>
  <input type="button" value="Test" onclick="countTestButtonClick()"/>
  <input type="button" value="ClickedCount" onclick="getTestButtonClickedCount();"/>
</body>
</html>

count.js

var clickCount = 0;

function countTestButtonClick()
{    
    clickCount++;
}

function getTestButtonClickedCount()
{
   alert("Total Number of clicked : " + clickCount);
}

Help me about this. Thanks for your effort.

+2  A: 

Remove the JS from the newWindow and change to

 <input type="button" value="Test" onclick="opener.countTestButtonClick()"/>
 <input type="button" value="ClickedCount" onclick="opener.getTestButtonClickedCount();"/>

Alternatively if one window does not open the other, use a cookie to store the clicks, reading it in and updating it and storing it each time

mplungjan
its worked fine. Thanks mplungjan.
EswaraMoorthyNEC