Muhammed Bhikha
Muhammed Bhikha

Reputation: 4999

Jquery/Javascript : call a function outside of the file we are calling the function

I have a number of javascript plugins which are included in my HTML file, and are included successfully because I have checked. Heres the problem; Basically I want to create a function outside the HTML file, and then inside the HTML file load that particular function when the document is ready.

this is the function (outside) of the HTML file. As a stand alone JS file:

function do_anchor_scrolling() {
    $('#back_to_top').anchorScroll();
    $("#landing_link").anchorScroll();
    $("#menu_link").anchorScroll();
    $("#sauces_link").anchorScroll();
    $("#ranches_link").anchorScroll();
    $("#order_link").anchorScroll();
    $("#about_link").anchorScroll();
    $("#franchise_link").anchorScroll();
});

the function is called do_anchor_scrolling

How in JQuery can I say when the document is ready perform the do_anchor_scrolling function.

Upvotes: 0

Views: 196

Answers (3)

Bergi
Bergi

Reputation: 664307

Pass it as a handler into the ready method:

$(document).ready(do_anchor_scrolling);

Or even shorter:

$(do_anchor_scrolling);

Notice the function is not executed right there (no invoking parenthesis) - the function object itself is passed into the ready function.

Upvotes: 0

You can just do:

$.getScript("yourfile.js");//it grabs and executes yourfile.js
                           //so your function is now defined
do_anchor_scrolling();     //and you can call it

You can go without the $_getScript() call by just putting yourfile.js inside a <script> tag in <head>.

Upvotes: -1

pmandell
pmandell

Reputation: 4328

When the document is ready, call the function.

$(document).ready(function(){
    do_anchor_scrolling();
});

Upvotes: 2

Related Questions