I am throughly confused....why does this seemingly simple deal not work?

<script type="text/javascript">
<!--
window.onbeforeunload = function (){
	var checkboxImg = new Image();
	var checkboxImgUrl = "checkbox.php%3Flonger%3D" + reviews-chk-longer.checked +
			"%26discrete%3D" + reviews-chk-discrete.checked + 
			"%26natural%3D" + reviews-chk-natural.checked +
			"%26thicker%3D" + reviews-chk-thicker.checked +
			"%26permanent%3D" + reviews-chk-permanent.checked +
			"%26stamina%3D" + reviews-chk-stamina.checked +
			"%26harder%3D" + reviews-chk-harder.checked +
			"%26vascularity%3D" + reviews-chk-vascularity.checked +
			"%26drive%3D" + reviews-chk-drive.checked +
			"%26guarantee%3D" + reviews-chk-guarantee.checked +
			"%26trials%3D" + reviews-chk-trials.checked +
			"%26affordable%3D" + reviews-chk-affordable.checked;
	alert(checkboxImgUrl);
	checkboxImg.src = checkboxImgUrl;
}
// -->	
</script>

I get no alert when I do this....the same thing without all of the added strings works fine, ie:

<script type="text/javascript">
<!--
window.onbeforeunload = function (){
	var checkboxImg = new Image();
	var checkboxImgUrl = "checkbox.php";
	alert(checkboxImgUrl);
	checkboxImg.src = checkboxImgUrl;
}
// -->	
</script>

The alert pops just fine....what is up??? What am I missing?

I tried .concat the strings too...still same thing is happening...why?

This is a repost of (http://www.daniweb.com/forums/post1247153.html#post1247153) because I couldn't get anyone to help me there...because I started posting stuff there to try to get it to the top...and in the end no one helped me.

Recommended Answers

All 6 Replies

What am I missing?

I don't know what you are missing, but we are missing the HTML input element(s) needed to test the code.

My suggestion would be to output an alert (or better yet if you have FF use console.log() with firebug) for each of the checkboxes to ensure that you have each one. It is possible that one isn't defined and it's causing the whole thing to go funky. Like fxm stated some more code would be helpful. How are you getting each checkbox? Are they defined globally outside of your function? Are you certain the vars for the checkboxes are correct? (ie. where are you declaring "var reviews-chk-longer")

Maybe I'm being simple minded here, but "reviews-chk-longer.checked" doesn't look like valid Javascript to me. The reason the alert box isn't showing up is most likely that the Javascript interpreter is throwing an exception because you are trying to subtract the undefined variable "chk" from the undefined variable "reviews".

I'm guessing that you have a checkbox element in your HTML with the ID "reviews-chk-longer", and you are trying to find out if it is checked or not, is that correct?

If so, you are going to need to use

document.getElementById('reviews-chk-longer').checked

. If you are using a Javascript library such as prototype, you can shorten this to

$('reviews-chk-longer').checked

by using the global dollar-sign function to look up the checkbox element by its ID.

If you have IE8 you can debug the javascript using Tools/Developer Tools, I think that was installed by default anyway. You can step thru your code line by line and set watches on the variables.

reviews-chk-longer.checked

In the absence of a suitable with () {} construct (or equivalent code) all of the references like that are probably 'undefined' in the function.

As I side note, it appears that you are simulating a <submit>. I wonder why.

milo24x7 got it...thank you!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.