Closed Thread Icon

Topic awaiting preservation: Couple javascript questions as I get back into the groove (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=25491" title="Pages that link to Topic awaiting preservation: Couple javascript questions as I get back into the groove (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Couple javascript questions as I get back into the groove <span class="small">(Page 1 of 1)</span>\

 
Schitzoboy
Maniac (V) Inmate

From: Yes
Insane since: Feb 2001

posted posted 04-12-2005 15:45

I know that it use to be the norm to surround your javascript with HTML comments. Is this still necesary? Especially if you are importing your javascript from a file? I'm guessing no because I'd assume if the browser can't handle the js and need the comments it probably can't import them to begin with, but I'd like to hear a more educated jscripters thoughts =)

Does javascript support this:

code:
if (something) {
}
else if (something else) {
}



or does it need to be put like this:

code:
if (something) {
}
else {
if (something else) {}
}



reason I ask is I'm using a snippet from a list apart that is in the 2nd format. I don't know why someone would write code like that, but maybe its the only way supported? Or is there a difference in functionality between the two that eludes me?

Lastly, Is there anyway to get the name of the page that is running an imported javascript file? For instance to run a function only if I'm on index.html?

Thanks in advance,

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 04-12-2005 16:20

[quick_reply]

I don't know for the HTML comment around JS code, but I find them stupid and useless. I've never seen a browser displaying the content of a SCRIPT tag.

For the code syntax : both are correct. Use the one you prefer. But the 2nd syntax is clearer if you have/want to test several conditions when the 1st condition fails.

Finally, for the test of page, you can use document.location to do such tests.

[/quick_reply]

Schitzoboy
Maniac (V) Inmate

From: Yes
Insane since: Feb 2001

posted posted 04-12-2005 16:46

Wow P01 that was quick! Thanks for the tidbit about the comments, I'll get rid of 'em now, just wanted to make sure.

Thats what I thought about the syntax, Just didn't expect to see the first format and thought there might be a reason. Glad to have that cleared up too.

How preytell do you use document.location? What is it? a string? I looked around the gecko reference and found that it is similar to document.URL but .URL is in a string format so I can test like so:

code:
var thisUri = document.URL;	
if (thisUri.substring(thisUri.length-10) == "works.html") {
//.only do stuff for works.html here
}



The same code with document.location did not work though. I got an error saying thisUri.substring is not a function, so I assume .location isn't in string format. Though I find it interesting that these two statements produced the same results:

code:
alert(document.URL);
alert(document.location);



Should I be using document.location instead or is this way fine?

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 04-12-2005 17:06

The quickness is pure luck
Doh! I never heard about document.URL.

window.location ( sorry for misguiding you by saying document.location in my previous post ) is an object. Check the methods and properties of JavaScript's window.location Object.

Schitzoboy
Maniac (V) Inmate

From: Yes
Insane since: Feb 2001

posted posted 04-12-2005 17:35

Ahh, hehe, still I wonder what document.location is used for then since it is so similar to document.URL. Has to be a reason for having two of them. I look into window.location, but this document.URL method i put togethor seems to be doing fine. Also document.URL is read only, I'd hate to accidently set my window.location.

Couple more quick questions, I'm using this code to get my window width:

code:
function getWindowWidth() {

var windowWidth = 0;

if (typeof(window.innerWidth) == 'number') {

windowWidth = window.innerWidth;

}

else {

if (document.documentElement && document.documentElement.clientWidth) {

windowWidth = document.documentElement.clientWidth;

}

else {

if (document.body && document.body.clientWidth) {

windowWidth = document.body.clientWidth;

}

}

}

return windowWidth;

}



It works great except it includes the scrollbar . Nothing serious, but I'm using the width to center elements, and if there is a scroll bar the right margin appear smaller. Kinda bugs me. Is there a different document property I should use? or is there a way to detect if a scroll bar is on and get its width so I can subtract it from the windowWidth?

And finally a question on global variables. I read that variables in a function with var are local but without var are global, however the reverse is true if you put variables outside of a function. I tried to put a couple global declarations at the top of my .js file but they never seem to get initialized. Am I doing something incorrectly?

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 04-12-2005 17:48

For the width, you can use the offsetWidth and offsetHeight properties on the body. But I don't know/remember if they include the scrollbars And you may need to parseInt() them.

Variables initialized outside of a function ( whether you use var or not ) are globals, as they are initialized at the top level of scope.

« BackwardsOnwards »

Show Forum Drop Down Menu