How to Call .js file(functions) at onclick image

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amritpalpathak
    New Member
    • Oct 2010
    • 17

    How to Call .js file(functions) at onclick image

    This is the html file
    Code:
    <html>
    <body>
    <body onload="changeIt();">
    <head>
    <script langauge="javascript">
    function changeIt()
    {
    
    var width=50;
    var height=50;
    document.image.width=width
    document.image.height=height
    }
    </script>
    </head>
    <img src="/home/administrator/Desktop/aa.jpeg" name="image" onclick=src="/var/www/external.js/changeeIt();"> 
    </body>
    </html>
    And this is the .js file

    Code:
    function changeeIt()
    {
    if(count==1)
    {
    var width=500
    var height=500
    document.image.width=width
    document.image.height=height
    count=2;
    }
    else if(count==2)
    {
    var amrit1=50
    var pal=50
    document.image.width=amrit1
    document.image.height=pal
    count=1;
    }
    }


    onclick event is not working .....
    Last edited by Dormilich; Oct 10 '10, 09:14 AM. Reason: please use [code] [/code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    your onclick definition is wrong.

    the syntax is onclick="functi on_name();", period. you cannot pass anything else (like where the function is defined, that doesn’t make sense to the JavaScript parser)

    Comment

    • amritpalpathak
      New Member
      • Oct 2010
      • 17

      #3
      onclick="functi on_name();

      this is for that if the function is defined at the same page... but here i have a .js file which have 2 functions and i want to use those function at image click...

      Thanks

      Comment

      • dgourd
        New Member
        • Apr 2010
        • 25

        #4
        What you have to do is include the javascript file, preferably between the <head></head> tags. Then you would set the onclick to the function. For example,
        Code:
        <head>
            <script language="javascript" src="path/to/external.js"></script>
        </head>
        <body>
            <img src="path/to/image.gif" onclick="function_name()" />
        </body>

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          this is for that if the function is defined at the same page
          incorrect, JavaScript looks for the function in all available sources, including external files.

          Comment

          Working...