/    Sign up×
Community /Pin to ProfileBookmark

Keeping all the functions together

There are about half a dozen Javascript functions which almost every page of my site uses. At the moment, they make an immense block of code in the head of each page.

It seems more efficient to put all of the functions together in one page, then let each user-facing page call them from there as required.

Trouble is, I don’t know how to phrase the command to fetch the function. Writing the whole path in before the function name doesn’t seem to work.

I realise this must be very basic, but please help!
Thanks,
CAA

to post a comment
Full-stack Developer

3 Comments(s)

Copy linkTweet thisAlerts:
@TheBearMayDec 02.2003 — You could try putting all of the functions into one JS file (eg. myFunctions.js) and then place the line below in the <head> section:

<script src='[i]myFunctions[/i].js'></script>

Then reference the functions normally. The JS file would contain everything normally found [b]between[/b] the <script> and </script> tags.
Copy linkTweet thisAlerts:
@AdamGundryDec 02.2003 — If you put the functions in a file on their own (only Javascript code in the file and with a .js extension), you can load the external script using:

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

Note that the script block should be completely empty. This will then load the .js file when execution reaches that point.

Adam

Edit: TheBearMay got there first, but note the syntax I gave is required for standards compliance.
Copy linkTweet thisAlerts:
@CherryAAauthorDec 05.2003 — It works beautifully, and all my pages are now about a quarter of the size!

Cheers ?

Cherry
×

Success!

Help @CherryAA spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...