Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Jun 2012
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    javascript code wont execute

    I am trying to execute this javascript here and get it to show the value in a paragraph. It works when i put it inside my html file, however as soon as a link to it in an external js file, it doesn't work. Why is this? Need help desperately! Thank you

    JS code multiplying a by b and then displaying the value in a paragraph with an id of "demo":

    Code:
    function myFunction(a,b)
        {
        return a*b; 
        }
                
        document.getElementById("demo").innerHTML=myFunction(4,3);
    Paragraph with id name "demo"

    Code:
    <p id="demo"></p>
    My link to my external stylesheet:

    Code:
    <script type="text/javascript" src="index.js"></script>
    What exactly is keeping this from working in an external javascript file? The javascript code is functional when placed within my HTML file, so why not my JS file?

    any help is appreciated, thanks.
    Last edited by Vonx; 02-02-2013 at 06:44 AM.

  • #2
    New Coder
    Join Date
    Jun 2012
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts
    i need an answer in the next 10 mins!

  • #3
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    851
    Thanks
    172
    Thanked 93 Times in 93 Posts
    Hi

    Your script is running before your demo para is loaded
    so it's looking for an id that does not yet exist.

    try placing your script to just before closing body tag


    <script type="text/javascript" src="index.js"></script>
    </body>
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,925
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Vonx View Post
    i need an answer in the next 10 mins!
    Do you? What about "please"?

    Two possible reasons why the external file will not work:

    a) It is placed in the wrong directory.
    b) It contains HTML tags such as <script>

    Also, the para with id "demo" must exist before the script is run. So you must place the call to your external file after the para. That is one reason why it is best to place scripts immediately before the </body> tag.

    It is your responsibility to die() if necessary….. - PHP Manual

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Luckily, both Low Tech and Phillip answered after the 10 minutes were up.

    <snicker style="evil" />
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    851
    Thanks
    172
    Thanked 93 Times in 93 Posts
    Luckily, both Low Tech and Phillip answered after the 10 minutes were up.

    <snicker style="evil" />
    Hahaha

    i need an answer in the next 10 mins!
    reminded me of the joke

    patient: doctor doctor, i've only got 59 secs to live!

    Doctor: wait a minute.


    seems 11 mins was just too late!
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,633
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Asking on a forum is not the thing to do when you need an answer that quickly. Particularly when you are asking on a weekend when the forum is quieter than during the week.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,925
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by felgall View Post
    Asking on a forum is not the thing to do when you need an answer that quickly. Particularly when you are asking on a weekend when the forum is quieter than during the week.
    I get the impression that quite a few newcomers imagine that unpaid experts are standing around 24/7 just to answer their questions.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #9
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Even worse are those who think it's okay to either do nothing on their own or to post a big chunk of code, of course without code tags and no formatting whatsoever, and be like "okay, now you do this for me".

    If you want someone to take the time to help, take the time to ask right.

  • #10
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    Quote Originally Posted by Old Pedant View Post
    <snicker style="evil" />
    I vote you for nerd of the day, Pendant

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,925
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by DanInMa View Post
    I vote you for nerd of the day, Pendant
    His handle is Pedant, not Pendant! A big difference!

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •