Solved: help with very simple Javascript

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

bob121

Thread Starter
Joined
Sep 10, 2006
Messages
207
Hey, I have put together a script which does exactly what i want. The first function gets the value after # in a url, then the second changes the state of a div.

Code:
<script language="javascript">


function gup(){
var regexS = "([\\#][^]*)";
var regex = new RegExp(regexS);
var results = regex.exec( window.location.href );
if(results == null) return "";
else return results[0].replace("#","");
} 

function ShowHideLayer(divID) {
	var box = document.getElementById(divID);	
		
	if(box.style.display == "none" || box.style.display=="") {
		box.style.display = "block"; 		
	}
	else {
		box.style.display = "none";		
	}
}

</script>
Both of the scripts function in Firefox. but I was hoping someone could help me make the two run together, So the value which the first function gets, is used for the second function

Thanks in advance.
 
Joined
Apr 30, 2005
Messages
3,012
The semi-colon ; is unneeded in JavaScript, it just makes the filesize of the document larger...
 
Joined
Mar 23, 2008
Messages
103
It's irrelevant whether the file is 10,000 bytes or 10,123 bytes and the semi-colons help in understanding the code. Once you've written a permanent code, you could just use Microsoft Word or something to remove the semi-colons.
 
Joined
Sep 17, 2008
Messages
21
Well my JS is a little rusty, but if they both work fine...

Run the first function and assign it to a variable.
Run the second function using the variable.

Place the following at the end of what you have:

Code:
var iPage = gup();
ShowHideLayer(iPage);
 

bob121

Thread Starter
Joined
Sep 10, 2006
Messages
207
That doesn't seem to work

<body onload="return ShowHideLayer('hello');">

Thats how i tested the second function which succeeded if thats any help.

"hello" been the name of the DIV.

Thanks James.
 

bob121

Thread Starter
Joined
Sep 10, 2006
Messages
207
I have managed to get it too work

Code:
function doall () {
var iPage = gup();
ShowHideLayer(iPage);

}

</script>

</head>
<body onload="return doall();">
 

MMJ

Guest
Joined
Oct 15, 2006
Messages
3,625
The semi-colon ; is unneeded in JavaScript, it just makes the filesize of the document larger...
Not exactly true, you should always put the semi-colon when writing js as without the semi-colon the js would break if the formatting was removed. (and you really shouldn't depend on the formatting)

Especially if you minify your js.
 

tomdkat

Retired Trusted Advisor
Joined
May 6, 2006
Messages
7,148
Not exactly true, you should always put the semi-colon when writing js as without the semi-colon the js would break if the formatting was removed. (and you really shouldn't depend on the formatting)
Great point. (y)

Peace...
 
Joined
Apr 30, 2005
Messages
3,012
Not exactly true, you should always put the semi-colon when writing js as without the semi-colon the js would break if the formatting was removed. (and you really shouldn't depend on the formatting)

Especially if you minify your js.
You only need the semi-colon if two commands are on the same line (so yes, what I said is true).

JavaScript reads white space
so
simply
going
to
a
new
line
does NOT require a semi-colon

document.write('Hello '); document.write('World')

OR

document.write('Hello ')
document.write('World')

same thing

since he spaced his JS, he didn't need the semi-colon

I'm not some JS noob, when I say the semi-colon isn't needed don't question me ;)
 

tomdkat

Retired Trusted Advisor
Joined
May 6, 2006
Messages
7,148
No, not a good point...
It is a good point because you shouldn't depend on formatting. Structuring your code based on how the JavaScript interpreter will read white space isn't a good practice, IMO. I believe MMJ shares that view and if he does I agree with him on that.

Peace...
 
Joined
Apr 30, 2005
Messages
3,012
It is a good point because you shouldn't depend on formatting. Structuring your code based on how the JavaScript interpreter will read white space isn't a good practice, IMO. I believe MMJ shares that view and if he does I agree with him on that.

Peace...
well w3schools.com would disagree with you. Because they make the same argument I made that the semi-colon is optional (thus, not required)

w3schools said:
The semicolon is optional (according to the JavaScript standard), and the browser is supposed to interpret the end of the line as the end of the statement. Because of this you will often see examples without the semicolon at the end.
http://w3schools.com/js/js_statements.asp
 

tomdkat

Retired Trusted Advisor
Joined
May 6, 2006
Messages
7,148
well w3schools.com would disagree with you. Because they make the same argument I made that the semi-colon is optional (thus, not required)
I'm not agreeing with MMJ about the requirement of the semi-colon, I'm agreeing with him on the point of not writing code in a manner that is dependent on things like formatting or idiosyncrasies of how the JavaScript interpreter works. It's more of a "good programming practice" kind of thing.

According to the quote you cited, the JavaScript "standard" indicates semi-colons are optional and then goes on to describe what the browser is supposed to do. Using semi-colons eliminates this entirely and allows you to write code that will work, regardless of JavaScript implementations (not counting extremely broken ones) and future specification changes. For example, what does the ECMAScript spec state about semi-colon use?

Peace...
 

MMJ

Guest
Joined
Oct 15, 2006
Messages
3,625
I'm not some JS noob, when I say the semi-colon isn't needed don't question me ;)

Find for me ONE person who is advanced in js who writes his code without semi-colons and i'll retract that statement.

FYI, I didn't say the semi-solon is required but it isn't unneeded.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Members online

Top