views:

23

answers:

1

Hello. In general... How can I make a call on a function of an external javascript 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

+3  A: 

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
Nick Craver
With firebug I get checkCookie is not defined error
Chocol8
@strakastroukas - Are you sure the relative path to your external file is correct, e.g. View -> Page source, click on it's link, does it come up correctly? For your current code, your page would have to be at the `~/Page.htm` level.
Nick Craver
That was it! I edited that to <script type="text/javascript" src="../JScript/FontSize.js"></script> (Added the ../ to the path)
Chocol8