CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   javascript code wont execute (http://www.codingforums.com/showthread.php?t=286909)

Vonx 02-02-2013 06:18 AM

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.

Vonx 02-02-2013 07:18 AM

i need an answer in the next 10 mins!

low tech 02-02-2013 07:29 AM

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>

Philip M 02-02-2013 07:36 AM

Quote:

Originally Posted by Vonx (Post 1310491)
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

Old Pedant 02-03-2013 04:29 AM

Luckily, both Low Tech and Phillip answered after the 10 minutes were up.

<snicker style="evil" />

low tech 02-03-2013 06:10 AM

Quote:

Luckily, both Low Tech and Phillip answered after the 10 minutes were up.

<snicker style="evil" />
Hahaha

Quote:

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!

felgall 02-03-2013 06:41 AM

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.

Philip M 02-03-2013 09:40 AM

Quote:

Originally Posted by felgall (Post 1310612)
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.

Airblader 02-03-2013 01:23 PM

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.

DanInMa 02-03-2013 04:02 PM

Quote:

Originally Posted by Old Pedant (Post 1310601)
<snicker style="evil" />

I vote you for nerd of the day, Pendant :thumbsup:

Philip M 02-03-2013 05:04 PM

Quote:

Originally Posted by DanInMa (Post 1310674)
I vote you for nerd of the day, Pendant :thumbsup:

His handle is Pedant, not Pendant! A big difference!


All times are GMT +1. The time now is 04:42 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.