5 Replies - 1695 Views - Last Post: 24 April 2011 - 08:49 PM

#1 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Something in my code is checking my checkbox, which I dont want

Posted 23 April 2011 - 07:16 PM

I am creating a simple page with some links. I have a checkbox on the page and I am trying to write the javascript to make it so a person can either click the link, and the new page opens in the same window, or they can click the checkbox, then the link will open in a new window.

The code seems to do that, except if the check box is not checked and you click a link, the checkbox becomes checked, and the link is opened in both a new window and the current window, which defeats the purpose.

here is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	
  <head>
	<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
	<title>My Links</title>
	<link rel="stylesheet" href="style.css" type="text/css" />
	<script language="javascript" type="text/javascript">
		/* <[CDATA[ */
		
		function showLink(target)
		{
			if (document.links.newwindow.checked = true)
			window.open(target, "Link_Window", "toolbar=no, 
                            menubar=no, resizable=yes, scrollbars=yes, 
                            height=800, width=600");
		}
			
		/* ]]> */
	</script>
		
</head>
	
  <body>
	<div id="wrapper">
		<h1>Simon's Favorite Links</h1>
		<form action="" name="links">
			<label>Check to open link in a new window</label>
			<input type="checkbox" name="newWindow"/>
			<ul>
			  <li><a href="http://www.dougrappoport.com" 
                               onclick="showLink('http://
                               www.dougrappoport.com')"> Doug 
                               Rappoport.com</a></li>
			  <li><a href="http://www.dice.com">Dice.com</a></li>
			  <li><a href="http://www.dreamincode.net">Dream-
                               In-Code forums</a></li>
			  <li><a href="http://www.pandora.com">Pandora
                                    Radio</a></li>
  			  <li><a href="http://validator.w3.org">W3C
                                    Validator</a></li>
		       </ul>
		</form>
	</div>
		
  </body>
	
</html>



This post has been edited by teahou: 23 April 2011 - 07:19 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Something in my code is checking my checkbox, which I dont want

#2 Prototypical  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 137
  • Joined: 20-April 11

Re: Something in my code is checking my checkbox, which I dont want

Posted 23 April 2011 - 07:58 PM

two things I notice off the bat involve this line :


if (document.links.newwindow.checked = true)


your checkbox is names "newWindow" -- you need to capitalize the w in the line above.

Second, you need to use == not = in your condition check

This post has been edited by Prototypical: 23 April 2011 - 08:02 PM

Was This Post Helpful? 1
  • +
  • -

#3 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Re: Something in my code is checking my checkbox, which I dont want

Posted 23 April 2011 - 08:15 PM

Excellent, that took care of the "auto checking" my box.

Now the only issue is the parent window changes to the new link, along with the new window opening, and I need the parent to stay put.

I can add 'return false' to the onclick event within the link, like this:

onclick="showLink('http://www.dougrappoport.com'); return false">Doug Rappoport</a></li>

but doing that causes it to not work unless the checkbox is checked (wont open in current window).

I can see the logic in my head, but i cant put it in code. It seems to me that the js script needs to have a else statement in there, so the function says, " If the checkbox is checked, open in a new window, else just open in the current window." The problem is, I am not sure how to code the 'else' part.

Any suggestions?
Was This Post Helpful? 0
  • +
  • -

#4 Prototypical  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 137
  • Joined: 20-April 11

Re: Something in my code is checking my checkbox, which I dont want

Posted 23 April 2011 - 08:23 PM

add a ";" to the return false in the onclick line.

Then add an else to your javascript and just navigate to the page as opposed to opening it in a new window.

window.navigate(target);

This post has been edited by Prototypical: 23 April 2011 - 08:27 PM

Was This Post Helpful? 1
  • +
  • -

#5 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Re: Something in my code is checking my checkbox, which I dont want

Posted 23 April 2011 - 08:41 PM

that did the trick! thank you v much.
Was This Post Helpful? 0
  • +
  • -

#6 sas1ni69  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 431
  • Joined: 04-December 08

Re: Something in my code is checking my checkbox, which I dont want

Posted 24 April 2011 - 08:49 PM

Hi,

May I suggest using a debugging extension on Firefox called Firebug? It'll help you narrow down the errors and it'll let you learn how to debug your code. Hope it helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1