Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a function of en external javascript file

In general... How can I make a call on a function of an external java script file?

More specific...

  • In the head tag i have

<script type="text/javascript" src="JScript/FontSize.js"></script>

  • The external javascript file, (that i would like to call) FontSize.js contains the following functions.

    function checkCookie()
    
    function setCookie(c_name, value, expiredays)
    
    function getCookie(c_name)
    
    function increaseFontSize()
    
    function decreaseFontSize()`
    
  • The FontSize.js is located at the ~/Jscript/ directory

I guess the body on load should contain something like

<body onload="/JScript/Fontsize.js/checkCookie()">

Of course nothing works as it should because, i do not know how to make the call to a function to an external js file

like image 713
OrElse Avatar asked May 29 '10 12:05

OrElse


People also ask

How do you call a function in an external JavaScript file?

Calling a function using external JavaScript file Once the JavaScript file is created, we need to create a simple HTML document. To include our JavaScript file in the HTML document, we have to use the script tag <script type = "text/javascript" src = "function.

Can JavaScript functions be created in an external file?

JavaScript functions can be created and stored in external files. Existing JavaScript libraries can also be linked into a component or . a5w page.

How do you call a function in JavaScript?

The JavaScript call() Method The call() method is a predefined JavaScript method. It can be used to invoke (call) a method with an owner object as an argument (parameter). With call() , an object can use a method belonging to another object.

How do you call a JavaScript function from HTML?

Step 1: Firstly, we have to type the script tag between the starting and closing of <head> tag just after the title tag. And then, type the JavaScript function. Step 2: After then, we have to call the javaScript function in the Html code for displaying the information or data on the web page.


2 Answers

You just call it as if it were local :)

<body onload="checkCookie()">

Or, do it in script:

window.onload = checkCookie;

When you declare a function and it's not in another object/namespace, it's just globally available, and you can call it as if it immediately preceded your current code. By default these functions will be on the window object, you can see a short demo here.

For example (doesn't matter where this function's defined, external or not):

function myFunc() { alert('hi'); }
myFunc();
window.myFunc(); //same call, unless there's *another* myFunc in a local-er scope
like image 55
Nick Craver Avatar answered Sep 20 '22 23:09

Nick Craver


  <html>
        <head>
            <script type="text/javascript" language="javascript" src="main.js"></script>
        </head>
        <body>

    <!--The extranal main.js file contains samp() function.. -->
            <script>
              <!--    samp(); -->
            </script>
        </body>
    </html>
like image 36
Gowtham Avatar answered Sep 20 '22 23:09

Gowtham