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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    9
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Exclamation Code For Writing OnClick In JS Files..???

    Hai Guys,

    ANyone Can Give Me Sample Code For OnClick Operation In JS File..



    Thanks:

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Event handlers such as onclick are not part of Javascript but are part of HTML. And you cannot have any HTML in a script or .js file.


    Customer in UK travel agent shop: - "What month is it now in Australia?"

  • Users who have thanked Philip M for this post:

    mantix (03-24-2009)

  • #3
    Regular Coder
    Join Date
    Dec 2008
    Location
    Tannhäuser Gate
    Posts
    286
    Thanks
    7
    Thanked 58 Times in 57 Posts
    Well, they are a part of Javascript too. Here's the example Javascript file:
    test.js
    Code:
    function example(){alert('Clicked!')}
    function inthejsfile(){
    	for(i=0;i<document.links.length;i++){
    	document.links[i].onclick=example;
    	}
    }
    window.onload=inthejsfile;
    and example HTML file:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title></title>
    <script type="text/javascript" src="test.js"></script>
    </head>
    <body>
    <a href="">Example link 1</a>
    <a href="">Example link 2</a>
    <a href="">Example link 3</a>
    </body>
    </html>
    Functions in .js file attach simple event handler to all links in the document, this happens after the document has been loaded.

  • Users who have thanked freedom_razor for this post:

    mantix (03-25-2009)

  • #4
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,637
    Thanks
    6
    Thanked 1,004 Times in 977 Posts
    In fact, in order to have unobtrusive JavaScript (which is part of the progressive enhancement approach) you must write such events in an external JavaScript, not in the HTML.


  •  

    Posting Permissions

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