1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

When Onclick ..call function from external .js script - How?

Discussion in 'JavaScript' started by chaser7016, Nov 29, 2008.

  1. #1
    Hi

    I want to take all the javascript code in my html file and put it into external .js file. Currently the javascript code is inside the html and when a button is pushed(onclick) it works.

    When I copy and paste the javascript and put into a test.js file to call it within the head of the html like this <script src='test.js' type='text/javascript' /></script> and then hit button ( <input type="button" onclick="setAlarm()" value="Set Alarm" /> ) FireBug says setAlarm is not defined.

    Within the test.js is function setAlarm() . Again I just copy/pasted this exact function from the html where it was working before but the button code was <input type="button" value="Set Alarm" onclick="javascript: setAlarm();" />. So works within html but when called from test.js it is not.

    Any idea why this is and what I might be missing?
    SEMrush
    Thanks
     
    chaser7016, Nov 29, 2008 IP
    SEMrush
  2. misbah

    misbah Active Member

    Messages:
    265
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    68
    #2
    working for me..., when I try this script

    index.html
    <html>
    <head>
    </head>
    <script src='test.js' type='text/javascript' /></script>
    <body>
    <input type="button" onclick="javascript: setAlarm();" value="10" />
    </body>
    </html>
    Code (markup):
    test.js
    function setAlarm()
    {
    	alert('hai');
    }
    Code (markup):
    please check again your file... (on same folder or not)
     
    misbah, Nov 30, 2008 IP