SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Jan 2009
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Calling Jquery function in separate file?

    Calling Jquery function in separate file?

    Hi all

    I'm sure this is simple but I can't work it out.

    I have an html file that includes jquery and javascript functions file (functions.js) that contains a simple jquery function.

    HTML
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
      <meta name="robots" content="noindex, nofollow" />
      
    	<title></title>
    	
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
      <script src="js/functions.js" type="text/javascript"></script>
      
      <script type="text/javascript" charset="utf-8">
        Tester('here');
      </script>
       
    </head>
    
    <body>
        
    </body>
    </html>
    functions.js
    Code:
    $(document).ready(function(){
    
    	function Tester(name){
        alert(name);
      }
    	
    	//Tester('here');
    	
    });
    The jquery function works if called from the bottom of the function.js file.

    How can I call the function from the html page?

    I tried with

    Code:
    <script type="text/javascript" charset="utf-8">
      Tester('here');
    </script>

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,192
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You currently have your Testing function wrapped inside the jQuery DOM ready function which isn't part of the global scope, simply move it outside the DOM ready function and it will work fine.

  3. #3
    SitePoint Addict
    Join Date
    Jan 2009
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks SgtLegend

    Do I need the jQuery DOM ready function at all ?

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,192
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Not at the moment, when you begin writing your jQuery code then you can use the DOM.ready() method.


    Sent from my iPhone using Tapatalk


Bookmarks

Posting Permissions

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