www.webdeveloper.com
Results 1 to 12 of 12

Thread: [RESOLVED] Undefined variable

  1. #1
    Join Date
    Jul 2011
    Posts
    131

    resolved [RESOLVED] Undefined variable

    Hi!
    I have problems with js in IE. I have such script:
    Code:
    <script type="text/javascript">
     $(document).ready(function(){
            cl = new commentsController();
    }
    </script>
    After that I have an onclick event:
    Code:
    <a href="#" class="write writereview" onClick="return cl.addCommentUnauth();">
        <?php echo $msg['write_a_review']; ?>
    </a>
    commentController is defined in an external file. This works fine in all browsers except IE. In IE I have error 'cl' is undefined.
    I tried to connect comment-controller file
    Code:
    <script type="text/javascript" src="/media/js/comments/comments-controller.js">
     $(document).ready(function(){
            cl = new commentsController();
    }
    </script>
    but then I have the same error in firefox too.
    Please help me to resolve this.
    Thank you!

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,273
    What is this comments-Controler.js that you used apparently with jQuery ?

  3. #3
    Join Date
    Apr 2010
    Location
    Salem,Ma
    Posts
    623
    try declaring the variable , IE can be picky about that.

    Code:
    <script type="text/javascript" src="/media/js/comments/comments-controller.js">
     $(document).ready(function(){
           var cl = new commentsController();
    }
    </script>

  4. #4
    Join Date
    Mar 2011
    Posts
    61
    Code between scripttags pointing to an external file isn't executed.

    Code:
    <script type="text/javascript" src="/media/js/comments/comments-controller.js"></script>
    
    <script type="text/javascript" >
     $(document).ready(function(){
            cl = new commentsController();
    }
    </script>

  5. #5
    Join Date
    Apr 2010
    Location
    Salem,Ma
    Posts
    623
    wow, I must be tired to have missed that one.
    Quote Originally Posted by Kever View Post
    Code between scripttags pointing to an external file isn't executed.

    Code:
    <script type="text/javascript" src="/media/js/comments/comments-controller.js"></script>
    
    <script type="text/javascript" >
     $(document).ready(function(){
            cl = new commentsController();
    }
    </script>

  6. #6
    Join Date
    Jul 2011
    Posts
    131
    I've already tried all these methods but nothing helps.
    If I write such script:
    Code:
    <script type="text/javascript" src="/media/js/comments/comments-controller.js"></script>  
    <script type="text/javascript" >  
        $(document).ready(function(){         
            cl = new commentsController(); 
        } 
    </script>
    IE says that 'commentsController' is undefined
    comment-controller.js is:
    Code:
    commentsController = function(){
        this.object_name = '';
        this.object_id = 0;
        this.current_order_type = '';
        this.current_count = 0;
        this.init = function(object_name, object_id, order_type, current_count) {
            this.object_name = object_name;
            this.object_id = object_id;
            this.current_order_type = order_type;
            this.current_count = current_count;
            this.initSlider();
        }
        this.addCommentUnauth = function() {
            currentPage.showLoginBox(not_auth_marking_comments); 
            return false;
        };
    ......................................................................................
    };

  7. #7
    Join Date
    Apr 2010
    Location
    Salem,Ma
    Posts
    623
    then you probably need to declare your variables properly

    commentsController = function(){

    should be

    var commentsController = function(){

    and cl = new commentsController();

    should be:

    var cl = new commentsController();

  8. #8
    Join Date
    Jul 2011
    Posts
    131
    still in onclick event IE says that 'cl' is undefined, though it is declared properly in this file.
    and also i have error in comment-controller.js when I wrote
    var commentsController = function(){
    Code:
        this.initSlider = function() {
             $('#customer_slider').slider({
                range:    'min',
                min:      0,
                max:      21,
                step:     1,
                slide:    function(event, sl){cl.slideValue(this, sl); return true},
                change:   function(event, sl){cl.slideValue(this, sl); return true}
            });
    }
    error stayed the same 'cl' is undefined but now it is also in firefox
    Last edited by kromol; 09-19-2011 at 05:28 PM.

  9. #9
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,273
    You have probably to declare cl as global variable and not only in the anonymous function.

    HTML Code:
    <script type="text/javascript">
    var cl;
    $(document).ready(function(){
            cl = new commentsController();
    }
    </script>

  10. #10
    Join Date
    Mar 2011
    Posts
    65
    Hi,

    as Julien said, variables declared within functions are only visible within these funcs.

    If you want them to be visible outside, just declare them on the top of your JS code and not within any function.

    eg:
    HTML Code:
    var a;
    
    function myFunc()
    {
       var b = "Hello";
             a = "Hello";
    }
    
    alert(a); //Alerts hello
    alert(b); //undefined -> error

  11. #11
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    Declaring variables with var is definitely good practice, but it doesn't seem to be the cause of the issue here.

    kromol, you started to trace the real source of the error: cl is undefined because commentsController is undefined. The real error is somewhere in comments-controller.js. You'll need to post that whole file if we're going to help you spot the error. Or, even better, post a link to the page so we can use dev tools to troubleshoot.

    EDIT: Actually, it's probably essential that you post a link to the page. The global variables make it impossible to look at just one script file in isolation.
    Last edited by Jeff Mott; 09-19-2011 at 08:08 PM.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  12. #12
    Join Date
    Jul 2011
    Posts
    131
    Declaring cl as global variable solved the problem.
    Thank You!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles