4 Replies - 1081 Views - Last Post: 06 September 2012 - 09:07 AM

#1 drayarms  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 175
  • Joined: 18-May 11

Is is Possible to Dump all Global Variables from a Script?

Posted 01 September 2012 - 02:09 PM

I have this jQuery based game which has already been bundled into a website. I'll provide the link here (www.playcheckonline.com) so that you may easily follow the problem. It's a very simple game, the rules are well explaine. It's quite
similar to UNO. The games works pretty good, at least according to the rules. But the rules somewhat break down whenever the you finish the first game and decide to play a second by not refreshing the browser, but rather by clicking
either the "quit" or "play again" link that pops up at the end of every game. i strongly feel that the reason is because there are some global variables(I used a great deal of them) which I haven't been able to locate, that are still hanging around the script
when each game concludes. I have made an effor to fish out all the ones I can find and explicitly delete them upon the end of each game round, but in a nearly 3000 line long script, there are bound to be some that would go unnoticed. So is there any javasript
function that can dump all the global variables in a script? Or alternatively, is there any function that deletes all global variables in the script? I have presented a skeleton of the script below, just in case there is something about the structure of the script itself that anyone can
noticed and that i have overlooked. Hopefully, the comments do a good job of explaining what is going on.







$(document).ready(function(){


	$("#start_game").click(function(){//Set things into motion when the start game link is clicked


		var pattern = "^([0-9]|-|')+$";

		var num_cards = $("#num_cards").val(); 

		if( (!num_cards.match(pattern)) ||(num_cards > 13) ){ //If the current value doesn't match accepted pattern ie only numbers or exceeds 13

			$("#start_game").hide();//Hide the start game button

			$("#warning_number").show();//show red warning box

			return;

		}//End if clause



		var game_number = 1;//Initialize a variable to indicate if this is the first or second or third... etc time we are playing


		main()//Parse the main function, which is defined below. 






		function main(){



			if(game_number == 1){//If this is the first game


	
				$("#pre_game_dialog").hide();//Hide the form  

				$("#play_check").fadeIn(500);//Display the game playing area 

				$("body").css({'overflow':'hidden'});//Remove scroll bars




			}//End if game number is 1






			//All functions and variables that  make the game possible, go here.  When either side(computer or you)is done, a global variable called "gamed"
			//is declared which can either take value "me"(in case you won) or "computer"(in case computer won).  This variable is then use in the next function(game over) defined below to reset the game 


			

			function game_over(){


				//Remove all elements from play area for example

				$("#player2_container").children().remove()...etc

			
				//Flush all active globals for example

				delete window.p1card_to_play

				delete window.card_on_board...etc

				window.setTimeout(function(){


					if(gamed == "me"){//Create a game over message as well as the quit and play again links on the fly

						var game_over = $("<div style = 'margin:auto;width:500px;position:relative;top:30px;z-index:1000;'> <div style = 'position:relative;top:100px;color:blue;font-size:26px;font-weight:600;font-family:verdana;' align = 'center'> You Win. Congratulations! </div>  <div style = 'margin:auto;width:220px;height:100px;position:relative;top:150px;'> <div id ='game_over_play_again' style = 'float:left;cursor:pointer;color:green;font-size:16px;font-weight:400;font-family:verdana;'>Play Again</div> <div id = 'game_over_quit' style = 'float:right;cursor:pointer;color:red;font-size:16px;font-weight:500;font-family:verdana;'>Quit</div> </div> </div>");

						$("#play_check").prepend(game_over);

					}//End if game is me

	
					if(gamed == "computer"){//Create a game over message as well as the quit and play again links on the fly

						var game_over = $("<div style = 'margin:auto;width:500px;position:relative;top:30px;z-index:1000;'> <div style = 'position:relative;top:100px;color:blue;font-size:26px;font-weight:600;font-family:verdana;' align = 'center'> Computer Wins! Sorry. </div>  <div style = 'margin:auto;width:220px;height:100px;position:relative;top:150px;'> <div id = 'game_over_play_again' style = 'float:left;cursor:pointer;color:green;font-size:16px;font-weight:400;font-family:verdana;'>Play Again</div> <div  id = 'game_over_quit' style = 'float:right;cursor:pointer;color:red;font-size:16px;font-weight:500;font-family:verdana;'>Quit</div> </div> </div>");

						$("#play_check").prepend(game_over);

					}//End if game is computer




					$("#game_over_quit").click(function(){//Reset game and display form again if quit link is clicked

						game_over.remove();

						delete window.gamed;//Delete this last global



						$("#play_check").hide();//Hide the play area

						$("#pre_game_dialog").show();//Display the form

						$("body").css({'overflow':'visible'});//Make scroll bars visible again


					});//End game over quit click function



					$("#game_over_play_again").click(function(){//If play again likn is clicked

						game_number++//Increment this variable

						game_over.remove();

						delete window.gamed;		
			

						//Run the main function to play again without having to refill the form
	
						main();	


					});//End play_again click function


				},300);//End window set timeout




			}//End of game over 











		}//End main


	});//End start game click method



});//End document ready method





Is This A Good Question/Topic? 0
  • +

Replies To: Is is Possible to Dump all Global Variables from a Script?

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 204
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Is is Possible to Dump all Global Variables from a Script?

Posted 01 September 2012 - 03:01 PM

Can't find the one I wrote, but managed to find a pretty decent one in the Web Bookmarks (I think it's Chrome, Firefox, Opera only). If I find the time I may slap together a new one that's more cross browser compatible, but for now:
javascript:(function(){var x,d,i,v,st; x=open(); d=x.document; d.open(); function hE(s){s=s.replace(/&/g,"&amp;");s=s.replace(/>/g,"&gt;");s=s.replace(/</g,"&lt;");return s;} d.write("<style>td{vertical-align:top; white-space:pre; } table,td,th { border: 1px solid #ccc; } div.er { color:red }</style><table border=1><thead><tr><th>Variable</th><th>Type</th><th>Value as string</th></tr></thead>"); for (i in window) { if (!(i in x) ) { v=window[i]; d.write("<tr><td>" + hE(i) + "</td><td>" + hE(typeof(window[i])) + "</td><td>"); if (v===null) d.write("null"); else if (v===undefined) d.write("undefined"); else try{st=v.toString(); if (st.length)d.write(hE(v.toString())); else d.write("%C2%A0")}catch(er){d.write("<div class=er>"+hE(er.toString())+"</div>")}; d.write("</pre></td></tr>"); } } d.write("</table>"); d.close(); })();


Inject it through the Address Bar and it will open a new page listing all the variables with values.
Was This Post Helpful? 0
  • +
  • -

#3 drayarms  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 175
  • Joined: 18-May 11

Re: Is is Possible to Dump all Global Variables from a Script?

Posted 01 September 2012 - 03:10 PM

View PostWilliam_Wilson, on 01 September 2012 - 03:01 PM, said:

Can't find the one I wrote, but managed to find a pretty decent one in the Web Bookmarks (I think it's Chrome, Firefox, Opera only). If I find the time I may slap together a new one that's more cross browser compatible, but for now:
javascript:(function(){var x,d,i,v,st; x=open(); d=x.document; d.open(); function hE(s){s=s.replace(/&/g,"&amp;");s=s.replace(/>/g,"&gt;");s=s.replace(/</g,"&lt;");return s;} d.write("<style>td{vertical-align:top; white-space:pre; } table,td,th { border: 1px solid #ccc; } div.er { color:red }</style><table border=1><thead><tr><th>Variable</th><th>Type</th><th>Value as string</th></tr></thead>"); for (i in window) { if (!(i in x) ) { v=window[i]; d.write("<tr><td>" + hE(i) + "</td><td>" + hE(typeof(window[i])) + "</td><td>"); if (v===null) d.write("null"); else if (v===undefined) d.write("undefined"); else try{st=v.toString(); if (st.length)d.write(hE(v.toString())); else d.write("%C2%A0")}catch(er){d.write("<div class=er>"+hE(er.toString())+"</div>")}; d.write("</pre></td></tr>"); } } d.write("</table>"); d.close(); })();


Inject it through the Address Bar and it will open a new page listing all the variables with values.


@ William Wilson, thanks for that. I'll try it.
Was This Post Helpful? 0
  • +
  • -

#4 drayarms  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 175
  • Joined: 18-May 11

Re: Is is Possible to Dump all Global Variables from a Script?

Posted 06 September 2012 - 09:07 AM

View PostWilliam_Wilson, on 01 September 2012 - 03:01 PM, said:

Can't find the one I wrote, but managed to find a pretty decent one in the Web Bookmarks (I think it's Chrome, Firefox, Opera only). If I find the time I may slap together a new one that's more cross browser compatible, but for now:
javascript:(function(){var x,d,i,v,st; x=open(); d=x.document; d.open(); function hE(s){s=s.replace(/&/g,"&amp;");s=s.replace(/>/g,"&gt;");s=s.replace(/</g,"&lt;");return s;} d.write("<style>td{vertical-align:top; white-space:pre; } table,td,th { border: 1px solid #ccc; } div.er { color:red }</style><table border=1><thead><tr><th>Variable</th><th>Type</th><th>Value as string</th></tr></thead>"); for (i in window) { if (!(i in x) ) { v=window[i]; d.write("<tr><td>" + hE(i) + "</td><td>" + hE(typeof(window[i])) + "</td><td>"); if (v===null) d.write("null"); else if (v===undefined) d.write("undefined"); else try{st=v.toString(); if (st.length)d.write(hE(v.toString())); else d.write("%C2%A0")}catch(er){d.write("<div class=er>"+hE(er.toString())+"</div>")}; d.write("</pre></td></tr>"); } } d.write("</table>"); d.close(); })();


Inject it through the Address Bar and it will open a new page listing all the variables with values.



@William, I'm not quite sure what you meant by "injecting it into the address bar". Do you literally mean copy the entire code and pasting it into the address bar of the browser?
Was This Post Helpful? 0
  • +
  • -

#5 drayarms  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 175
  • Joined: 18-May 11

Re: Is is Possible to Dump all Global Variables from a Script?

Posted 06 September 2012 - 09:07 AM

View PostWilliam_Wilson, on 01 September 2012 - 03:01 PM, said:

Can't find the one I wrote, but managed to find a pretty decent one in the Web Bookmarks (I think it's Chrome, Firefox, Opera only). If I find the time I may slap together a new one that's more cross browser compatible, but for now:
javascript:(function(){var x,d,i,v,st; x=open(); d=x.document; d.open(); function hE(s){s=s.replace(/&/g,"&amp;");s=s.replace(/>/g,"&gt;");s=s.replace(/</g,"&lt;");return s;} d.write("<style>td{vertical-align:top; white-space:pre; } table,td,th { border: 1px solid #ccc; } div.er { color:red }</style><table border=1><thead><tr><th>Variable</th><th>Type</th><th>Value as string</th></tr></thead>"); for (i in window) { if (!(i in x) ) { v=window[i]; d.write("<tr><td>" + hE(i) + "</td><td>" + hE(typeof(window[i])) + "</td><td>"); if (v===null) d.write("null"); else if (v===undefined) d.write("undefined"); else try{st=v.toString(); if (st.length)d.write(hE(v.toString())); else d.write("%C2%A0")}catch(er){d.write("<div class=er>"+hE(er.toString())+"</div>")}; d.write("</pre></td></tr>"); } } d.write("</table>"); d.close(); })();


Inject it through the Address Bar and it will open a new page listing all the variables with values.



@William, I'm not quite sure what you meant by "injecting it into the address bar". Do you literally mean copy the entire code and pasting it into the address bar of the browser?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1