how use global variable in javascript for main and newwindow
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 coun开发者_如何学编程t 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.
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
精彩评论