Very beginner question - how does javascript work?

Discussion in 'JavaScript' started by jasoncdu, Sep 4, 2011.

  1. #1
    Hey all,

    I am just beginning javascript .. and I have no idea how it works. If you include a .js file with the proper codes inside, does the code automatically execute or do you have to refer to it in your html code (besides linking to the js file in the <head>)?

    Does the browser read the javascript code first?

    I am not sure if I am making any sense, but for example for css you need to state the particular class or id in order for those attributes to be applied to the div.

    How does javascript know when to activate it's features?

    Sorry if I am not making any sense but I am quite confused on how this entire javascript thing works too so don't know how to ask the question.

    If you need any clarification please let me know

    Thanks so much in advanced!
    Jason
     
    jasoncdu, Sep 4, 2011 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    Both. Javvascript outside a function is executed when it's reached. Functions are executed when they're called.

    First ... before what?

    It doesn't. You write code to call the functions you need when you need them to run - usually in response to events, like a click or a keypress.

    That's because you're learning Javascript but haven't learned programming yet. Javascript is just a programming language, a tool to use to write programs. Since you don't know how to write programs (that's programming, not a programming language) you don't know how to use Javascript (or any other language you may learn in the future) to write them. Learn programming first. Then learn how web pages and browsers work - the Document Object Model, client/server, etc. THEN learn some programming languages.
     
    Rukbat, Sep 4, 2011 IP
    jasoncdu likes this.
  3. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #3
    if a javascript code is inside the <head> it will be executed even before you see the page it self.. in case the javascript is inside the <body> it will be executed when the page rendering comes to that part of the page..

    by default, javascript is activated unless the browser of the viewer disabled it (it's controlled in the settings of the browser).

    this might help you in learning javascript..
     
    JohnnySchultz, Sep 7, 2011 IP
    jasoncdu likes this.