Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 10 of 10

Thread: JS and Comments

  1. #1
    Member
    Join Date
    Mar 2012
    Posts
    50
    Member #
    31189
    Hey there!

    I'm embarking on a sort of summer crash course on JS (and web design). I'm confused on html comment tags IN JS. According to W3 there are some browsers that don't/can't read JS; these browsers the code is put in html comment tags.

    example -
    <script type="text/javascript">
    document.write("<h>TEXT</h>");
    </script>

    SUPPOSE I take account for some browser that can't read this properly and make the following change:

    <script type="text/javascript">
    <!--
    document.write("<h>TEXT</h>");
    //-->
    </script>

    MY question: since I don't control what browser people use - do the comment tags (in this context) mean "always hide the stuff inside" or does it mean "if browser can't read then hide, otherwise run/execute." Hope that makes sense...

    Thanks all in advance -

    AG

  2.  

  3. #2
    Senior Member Webzarus's Avatar
    Join Date
    May 2011
    Location
    South Carolina Coast
    Posts
    3,322
    Member #
    27709
    Liked
    770 times
    All modern browsers support JavaScript.

    Depending on what the lowest level of support is... You will need to either find that browser... Or a browser emulator... To test your code with...

    Most current browsers will display what you wrote above as a "comment"... And will not parse or process the JavaScript. It wil only show in the "source" as a comment.


    JavaScript is a "client side" language... Meaning of the client doesn't understand it... It just ignores it... But some browsers will still write out the code as writte to the source... It just doesn't process it.

    For those that choose to turn off JavaScript... Well, those are few and far between...

  4. #3
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,485
    Member #
    425
    Liked
    2783 times
    What WZ said. If you use comments the way you are there, it's the same as doing // document.write ("<h>TEXT</h>"); in an external JavaScript.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  5. #4
    Member
    Join Date
    Mar 2012
    Posts
    50
    Member #
    31189
    Quote Originally Posted by Webzarus, post: 237441
    All modern browsers support JavaScript.
    I thought so. I'm trying to get used to the concept so I have to take a baby-step detour on occasion...

    Quote Originally Posted by Webzarus, post: 237441
    JavaScript is a "client side" language... Meaning of the client doesn't understand it... It just ignores it... But some browsers will still write out the code as writte to the source... It just doesn't process it.
    This part confused me a bit, so I want to be sure: writing out the code = literally showing the snippet as text on the page? correct?

    At the risk of sounding far too elementary: I assume it's always best to have js scripts as separate files with use <script src> tag...but how would I call the file (in the body)? I don't mean <script src> tag in <head>, rather in the <body>. Would I simply refer to the src file itself?

    Sorry just want to visualize the concept...

    Thanks

    AG

  6. #5
    Senior Member Webzarus's Avatar
    Join Date
    May 2011
    Location
    South Carolina Coast
    Posts
    3,322
    Member #
    27709
    Liked
    770 times
    Typically, depends on the size and amount of code and type of javascript as to how I handle it.

    These days, I typically keep it to minimum.

    If for instance I'm doing some validation... I create and external file ... Create a "function" that does all the validation...

    The on the form submit button OR form action ... I cann the function like...

    Onsubmit="myfunction()";

    You could do the same with pretty much everything... But if it's small... Then embedding functions externally ... Calling then ect... Makes it difficult for troubleshooting later on...

  7. #6
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,485
    Member #
    425
    Liked
    2783 times
    I do it slightly differently than WZ does it...at least from what I can understand of his explanation.

    I externalize all my JavaScript functions. They all go into a folder called "JavaScripts" and I create one for each page that requires one, with a file name that corresponds with the file name of the page it's being used for (e.g. a contact-us.html page would have a contact-us.js JavaScript). I make minimal calls to the functions on the page itself, and that's it. It's simple, it's organized, it's easy enough to follow, whenever I compile a site I can avoid compiling those files (I work in ASP.net), and it works.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  8. #7
    Member
    Join Date
    Mar 2012
    Posts
    50
    Member #
    31189
    Seems like 1264's method would be best for me...considering I'm very much still learning JS and validations look very frightening to me!

    I have a question about calling particular external scripts. Assume I have the following php page:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd"> //my default is transitional
    <HTML>
    <HEAD>
    <TITLE>page title</TITLE>
    <META http-equiv="Content-Script-Type" content="text/javascript"> //this isn't really important, but included for practice...it can be anything from JS to VB, etc.
    <SCRIPT type="text/javascript" src="../javascripts/A.js"> </SCRIPT>
    <SCRIPT type="text/javascript" src="../javascripts/B.js"> </SCRIPT>
    </HEAD>
    <BODY>
    <SCRIPT type="text/javascript">...some JavaScript...</SCRIPT>
    </BODY>
    </HTML>

    My question is, in the body, how does the client know which js file to run? Do I just repeat <script src=" ">.

    In that case, the body would be: <script type="text/javascript" src="../javascripts/A.js">...coding...</script>
    But isn't this redundant since it's already been "thrown into" the header as well? Am I missing something?

    Thanks,

    AG

  9. #8
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,485
    Member #
    425
    Liked
    2783 times
    First of all, you don't need that content-script-type meta tag. I've never even seen that before.

    As far as which external Javascript it calls, that depends on the nature of the function and/or code being called. Consider this example.

    a.js:
    Code:
    function a() {
        var x = 1 + 1;
        alert (x);
    }
    b.js:
    Code:
    function b() {
        var y = 2 + 2;
        alert (y);
    }
    Those are your two functions. Nice and easy, right?

    Then you have a page like this (removed the extra stuff for clarity):
    HTML Code:
    <body>
    <script type="text/javascript">a();</script>
    </body>
    The client machine will go through your external Javascripts until it finds a.js (in this case, in the first file) and then process it. As long as you make sure not to use the same function name in two or more of the Javascripts on a page, you should be fine.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  10. #9
    Member
    Join Date
    Mar 2012
    Posts
    50
    Member #
    31189
    Thanks for the reply!

    To confirm:

    (1) JS filename CORRESPONDS to the js function in the js file (like a.js would call function a() { coding } )

    (2) one .JS per JS function --> at least at my stage

    (3) in <body> <script type="text/javascript">a();</script> </body>
    I ALWAYS refer to the specific function [ a(); ] in the .js file that I want to call AND not the file name itself.
    I ignore the .js file name altogether in <body>

    Thanks,
    AG

  11. #10
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,485
    Member #
    425
    Liked
    2783 times
    1) and 2) It doesn't have to. You can have several functions in one JS file. The only reason I made them correspond is for clarity. You could call the function dinglesnort() and as long as you used <script type="text/javascript">dinglesnort()</script> it would still work. I guess that is pretty ambiguous on my part.

    If anything, you'll want to get all of your functions into one external Javascript file for load time purposes (fewer calls to the server).

    3) Yes. See 1 and 2.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 04:39 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com