OrElse
OrElse

Reputation: 9959

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...

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

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

Upvotes: 7

Views: 33247

Answers (2)

Gowtham
Gowtham

Reputation: 11

  <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>

Upvotes: 1

Nick Craver
Nick Craver

Reputation: 630399

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

Upvotes: 13

Related Questions