Jump to content

Five More Quick Questions


violagirl

Recommended Posts

1. Can anyone tell me why this won't work? I didn't have any better luck by using || either.

<html><body><script type="text/javascript"><!--var x = 10;var y = 2;if (y = 2 && x = 10){ document.write("Yay");};//--></script></body></html>

2. Also, for switches, if you don't HAVE a default, I'm assuming it's all right not to put break on the end of the last case because the switch is ending. Am I right in assuming this?3. Which of these is better?

 if (promptbox != null && promptbox != "")   {   greetings();  function greetings()	 {	 alert("Greetings, " + promptbox + "!");	 };   };

or

 if (promptbox != null && promptbox != "")   {   function greetings()	 {	 alert("Greetings, " + promptbox + "!");	 }; greetings();};

I would THINK the second one is better. Are there times when the first just absolutely won't work? It's just that it seems to me like you are calling the function before you are defining it, which seems impossible, but obviously not, because it works. :) So could anyone give me some insite on this?4. So when they say functions are always defined in the head, do they mean always always? Like there is absolutely no time they would be anywhere else? I think I can see why, as the point of them is so the code doesn't occur instantaneously, but still... was just wondering.5. Lastly, can someone please tell me why this doesn't work? I'm sure there's some idiotically obvious reason, but I'm just not seeing it:

<html><body><script="text/javascript"><!--var a = 5;var b = (a == 5) ? 3 : 2; document.write(b);//--></script></body></html>

Thanks!Megan

Link to comment
Share on other sites

1. use

if (y == 2 && x == 10)

== is use to compare = is used to assign2. I believe you have to have a default (other languages require it), you should put a break at the end of every case and at the end of default3. there is no diffrence. JS doesn't care.4. putting functions in the head is good practice but thats not to say they won't work if you put them somewhere else.5. <script="text/javascript"> should be <script type="text/javascript">

Link to comment
Share on other sites

2. I believe you have to have a default (other languages require it), you should put a break at the end of every case and at the end of default
You can have control fall through the cases too (in Javascript, but not in C# :)):
var test = 2;switch(test){	case 1:	case 2:	case 3:		// do something;	case 4:		// do something else;		break;	case 5:		// do something completely different;		break;	default:		break;}

4. putting functions in the head is good practice but thats not to say they won't work if you put them somewhere else.
I think they are usually declared in the head because the head is parsed by the browser before the body. If you have an element in the body which calls a function, that function will need to have been declared or else you'll get an error. Putting the functions in the head helps prevent this from happening.
Link to comment
Share on other sites

I think they are usually declared in the head because the head is parsed by the browser before the body. If you have an element in the body which calls a function, that function will need to have been declared or else you'll get an error. Putting the functions in the head helps prevent this from happening.
Also, you might want to put the function in the body if you have it alter HTML that is after the head element :)
Link to comment
Share on other sites

Also, you might want to put the function in the body if you have it alter HTML that is after the head element :)
I typically declare all of my functions in the head - rather, I typically put them in a separate js file and link to them from the head. Then, if I have a function which uses the DOM to affect the body, I wait to execute that function until the onload event fires:
<head><script type="text/javascript">function updateDiv(){	document.getElementById("myDiv").innerHTML = new Date();}window.onload = updateDiv;</script></head><body>  <div id="myDiv"></div></body>

Link to comment
Share on other sites

Yea, i see where your coming from, and that's most likely the best method, as all of your scripts are in the head element. But i like to just keep the function inside one script tag rather than create another one to execute it, or using window.onload and setTimeout :)Also, most of my functions execute themselves :) So i cant really do it later on in the pages source :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...