[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!
What is this comments-Controler.js that you used apparently with jQuery ?
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>
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>
wow, I must be tired to have missed that one.
Originally Posted by
Kever
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>
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;
};
......................................................................................
};
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();
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 04:28 PM .
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>
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
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 07: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";
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
Forum Rules
Bookmarks