0

I have a file called function.js which has all my jQuery for my aplication which looks like this

$(document).ready(function(){

    insert_initial(); //first time to the page, insert into cart and set the subtotal originally

    function update_gallery(product_id){
        ...
    }

    function update_prices(product_selector){
        ...
        ...
    }

    function insert_initial(){
        ...
    }

    $('.trigger').click(function(){
        $('.stations').find(".drop-down").slideToggle();
        return false;
    });

    ...
    ...

On the top of the file i have my function call insert_initial(); which gets run on the initial load....and this works great..My problem is that i now need to include this js file on my php pages say 1.php and 2.php and 3.php and 1.php is the only one that needs the insert_initial(); ....so i was thinking of the best way to do this. I assumed taking out the function call out of the functions file and putting it into a separate file

<script src="/someting/js/functions.js" type="text/javascript"></script>
<script src="/someting/js/functions_insert.js" type="text/javascript"></script> 

and in my functions_insert.js file i would have only

$(document).ready(function(){
insert_initial(); //first time to the page, insert into cart and set the subtotal originally
});

but that didnt work either...any ideas on how to fix this

4 Answers 4

1

This checks to make sure that the location of the current page includes "1.php" before calling insert_initial():

if(window.location.href.indexOf('1.php') != -1)
    insert_initial();
Sign up to request clarification or add additional context in comments.

Comments

1

I would recommend having your definitions and executions separate in this instance. You don't need to define your functions inside of jQuery's DOM ready event. But it is also good to namespace them as mentioned. A common paradigm I follow is like so:

functions.js

(function($, window, undefined) {
    function update_gallery(product_id){
        ...
    }

    function update_prices(product_selector){
        ...
        ...
    }

    function insert_initial(){
        ...
    }

    window.MyApp = {
        update_gallery: update_gallery,
        update_prices: update_prices,
        insert_initial: insert_initial
    };
})(jQuery, window); 

1.php

<script src="functions.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
    MyApp.insert_initial();
});
</script>

Now you can include your definitions as needed, and call them as necessary.

Comments

1

Try namespacing your functions and attaching them to a nice global object.

window.MyApp = {};

MyApp.insert_initial = function(){

};

Then you can access it from wherever you need, provided it's included earlier in the page.

Edit:

If this doesn't work, you've got an error elsewhere in your code - load order, perhaps? Either method you've described to invoke the function is fine, just make sure it's defined when you invoke it.

2 Comments

how do i call this...i tried this window.MyApp = {}; MyApp.insert_initial(){ .... };
and im calling it will this $(document).ready(function(){ MyApp.insert_initial(); });
0

Your functions defined in functions.js are only visible in the scope of that document ready function. A simple case where it doesn't work:

(function() {
    function square(x) {
        return x*x;
    }
})();

alert(square(2)); //Fails, since square is not in scope

The easiest way to fix this is to declare your functions in the global namespace:

function square(x) {
    return x*x;
};

alert(square(2)); //4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.