1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: help with very simple Javascript

Discussion in 'Web Design & Development' started by bob121, Sep 29, 2008.

Thread Status:
Not open for further replies.
Advertisement
  1. bob121

    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.
     
  2. namenotfound

    namenotfound

    Joined:
    Apr 30, 2005
    Messages:
    3,012
    The semi-colon ; is unneeded in JavaScript, it just makes the filesize of the document larger...
     
  3. bob121

    bob121 Thread Starter

    Joined:
    Sep 10, 2006
    Messages:
    207
    PHP Habits
     
  4. lumentec

    lumentec

    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.
     
  5. Saikuron

    Saikuron

    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);
    
     
  6. bob121

    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.
     
  7. bob121

    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();">
    
     
  8. MMJ

    MMJ Guest

    Joined:
    Oct 15, 2006
    Messages:
    3,625
    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.
     
  9. tomdkat

    tomdkat Retired Trusted Advisor

    Joined:
    May 6, 2006
    Messages:
    7,148
    Great point. (y)

    Peace...
     
  10. namenotfound

    namenotfound

    Joined:
    Apr 30, 2005
    Messages:
    3,012
    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 ;)
     
  11. namenotfound

    namenotfound

    Joined:
    Apr 30, 2005
    Messages:
    3,012
    No, not a good point...
     
  12. tomdkat

    tomdkat Retired Trusted Advisor

    Joined:
    May 6, 2006
    Messages:
    7,148
    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...
     
  13. namenotfound

    namenotfound

    Joined:
    Apr 30, 2005
    Messages:
    3,012
    well w3schools.com would disagree with you. Because they make the same argument I made that the semi-colon is optional (thus, not required)

    http://w3schools.com/js/js_statements.asp
     
  14. tomdkat

    tomdkat Retired Trusted Advisor

    Joined:
    May 6, 2006
    Messages:
    7,148
    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...
     
  15. MMJ

    MMJ Guest

    Joined:
    Oct 15, 2006
    Messages:
    3,625

    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.
     
  16. Sponsor

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 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/754533

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice