Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-02-2013, 06:18 AM   PM User | #1
Vonx
New Coder

 
Join Date: Jun 2012
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Vonx is an unknown quantity at this point
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..
Vonx is offline   Reply With Quote
Old 02-02-2013, 07:18 AM   PM User | #2
Vonx
New Coder

 
Join Date: Jun 2012
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Vonx is an unknown quantity at this point
i need an answer in the next 10 mins!
Vonx is offline   Reply With Quote
Old 02-02-2013, 07:29 AM   PM User | #3
low tech
Regular Coder

 
low tech's Avatar
 
Join Date: Dec 2009
Posts: 740
Thanks: 149
Thanked 67 Times in 67 Posts
low tech is on a distinguished road
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>
__________________
Ask not what can I do for myself, but what can I do for others

"The greatest revenge is to accomplish what others say you cannot do."
~ Unknown
low tech is offline   Reply With Quote
Old 02-02-2013, 07:36 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 02-03-2013, 04:29 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 02-03-2013, 06:10 AM   PM User | #6
low tech
Regular Coder

 
low tech's Avatar
 
Join Date: Dec 2009
Posts: 740
Thanks: 149
Thanked 67 Times in 67 Posts
low tech is on a distinguished road
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!
__________________
Ask not what can I do for myself, but what can I do for others

"The greatest revenge is to accomplish what others say you cannot do."
~ Unknown
low tech is offline   Reply With Quote
Old 02-03-2013, 06:41 AM   PM User | #7
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,455
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 02-03-2013, 09:40 AM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 02-03-2013, 01:23 PM   PM User | #9
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 364
Thanks: 3
Thanked 43 Times in 43 Posts
Airblader can only hope to improve
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.
Airblader is offline   Reply With Quote
Old 02-03-2013, 04:02 PM   PM User | #10
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
Quote:
Originally Posted by Old Pedant View Post
<snicker style="evil" />
I vote you for nerd of the day, Pendant
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Old 02-03-2013, 05:04 PM   PM User | #11
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Tags
fix javascript code help

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:14 PM.


Advertisement
Log in to turn off these ads.