Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Javascript - in seperate file

Options
  • 03-12-2007 9:27am
    #1
    Registered Users Posts: 500 ✭✭✭


    folks,

    I have a javascript function that i want to seperate out into a different file. Now in my html i have added the
    <script type="text/javascript" src="test.js"></script> to the head. and perfect it calls it from the html using the onclick.
    <input type="submit" name="Submit" value="Submit" onclick="testing()">

    Now i also want to remove out the onclick="testing()" form the html page. I want all JS within the JS file.

    How do i do this?

    I know you can do something like this in your JS file to do it with a window.load

    function testing() {
    //testing code
    } window.onload=testing


Comments

  • Registered Users Posts: 21,257 ✭✭✭✭Eoin


    warrenaldo wrote: »
    folks,

    I have a javascript function that i want to seperate out into a different file. Now in my html i have added the
    <script type="text/javascript" src="test.js"></script> to the head. and perfect it calls it from the html using the onclick.
    <input type="submit" name="Submit" value="Submit" onclick="testing()">

    Now i also want to remove out the onclick="testing()" form the html page. I want all JS within the JS file.

    How do i do this?

    I know you can do something like this in your JS file to do it with a window.load

    function testing() {
    //testing code
    } window.onload=testing

    You want to look up adding event handlers. I think it's the best way to do it in theory, but not sure how widely supported it is. Here's a link to get your started.


  • Registered Users Posts: 3,594 ✭✭✭forbairt


    Here's a quick example I googled ...

    http://fiddlerelf.com/2007/06/03/how-to-attach-html-dom-events-to-specific-javascript-objects/

    You can see the example here ...

    http://fiddlerelf.com/closures/demo1/

    Its pretty much what you want I believe

    But to be honest I'd recommend you get jquery and play with that


  • Closed Accounts Posts: 1,200 ✭✭✭louie


    ... and what happens if the user has Javascript disabled? Can he/she still use the form?

    Evend listener is the way to go but make sure the form is usable no matter what.


  • Registered Users Posts: 21,257 ✭✭✭✭Eoin


    louie wrote: »
    ... and what happens if the user has Javascript disabled? Can he/she still use the form?

    Evend listener is the way to go but make sure the form is usable no matter what.

    Absolutely - but that would be an issue regardless of whether the trigger is called inline or inserted via an event handler.


Advertisement