1

hi have a php template that displays an html page. in it there is a call to a js file that initializes some jquery plugins.

on the template i have this code:

(function($) { 

 $(document).ready(function(){
     var adlow = <?php echo $jSeblod->disparities_tick_lower->value ?>;
     var adhigh = <?php echo $jSeblod->disparities_tick_upper->value ?>;
     var adtickno = <?php echo $jSeblod->disparities_tick_divisions->value ?>;

    });
})(jQuery);

then on the init.js page i call adlow, adhigh and adtickno. i get variable undefined errors.

the php is returning the correct values. but then the external init file is not getting them. do i need to do something special to get the init file to be able to use these variables? all i did was just plug them in...

thanks, i am a little hazy on the interaction between these pages.

2 Answers 2

2

They need to be globals for other pages to use them. Try removing the var. Or namespace them, which is better practice:

$(document).ready(function(){
    MYAPP = {};
    MYAPP.adlow = <?php echo $jSeblod->disparities_tick_lower->value ?>;
    MYAPP.adhigh = <?php echo $jSeblod->disparities_tick_upper->value ?>;
    MYAPP.adtickno = <?php echo $jSeblod->disparities_tick_divisions->value ?>;

   });
Sign up to request clarification or add additional context in comments.

3 Comments

hi, this is just what i meant. i didnt understand the whole global declaration thing. but balusc was correct as well. neither answer works without the other. you need to declare the globals before init.js loads.
just so i am clear here. the MYAPP is the new namespace? and any variable i declare with that namespace will then be available globally?
MYAPP is a global javascript object that is being used as a namespace. Since MYAPP is global, any variables added to it will be globally accessible as well, yes.
2

You need to print them in global scope before init.js get loaded.

<script>
    var adlow = <?php echo $jSeblod->disparities_tick_lower->value ?>;
    var adhigh = <?php echo $jSeblod->disparities_tick_upper->value ?>;
    var adtickno = <?php echo $jSeblod->disparities_tick_divisions->value ?>;
</script>
<script src="init.js"></script>

Printing them during $(document).ready() makes no sense. The init.js already get loaded before that.

1 Comment

hi, while your answer was needed, (see above comment)... the above really got to the heart of my question, which was about passing the variables. if i could check two correct i would check your answer too.

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.