1

I know I might be doing something wrong. Can anyone please point out why I'm getting top as object??

$(document).ready(function(){
    topwithpx='0px';
    alert(topwithpx);
    topstr=topwithpx.substr(0,topwithpx.length-2);
    alert(topstr);
    top=parseInt(topstr);
    alert(top);
});​

http://jsfiddle.net/kjMs9/

Thanks all: 'top' is reserved keyword (Window.top). My bad. Accepting first ans. +1 to all for quick ans.

6
  • 3
    Can you include your problem in this question? Otherwise it's going to get closed pretty quickly. Commented Aug 23, 2012 at 14:12
  • From your fiddle, I get top's value as 0. What is your question? Commented Aug 23, 2012 at 14:14
  • May I also suggest you look at JsLint jslint.com it will point out issues like this and others. Commented Aug 23, 2012 at 14:16
  • Thanks for edit mike. @Nivas: Really? In my chrome, I'm getting [object Window] as third popup. Please try in chrome. Commented Aug 23, 2012 at 14:16
  • 2
    Unrelated, but don't forget your radix parameter in parseInt() - you might end up with some unexpected results if you don't. Commented Aug 23, 2012 at 14:21

7 Answers 7

9

Because it's essentially window.top, which is Window object. Use var top instead to prevent mixing local variable with global (= properties of window object) ones.

In fact, make var-ing your function variables something of a common routine - to prevent getting similar gotchas in the future. )

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

4 Comments

shadowing a global variable is not a good suggestion, you should never name a variable top.
Thanks for any. My bad. Thats correct. +1 for quick ans to every one.
@jbabey Because of what? When JS parser will try to determine the variable's value, it always starts from the lowest (local) scope. I do agree that it's not a good idea to use top instead of window.top, though, if you address that property.
@raina77ow shadowing is a bad practice, but this is matter of opinion.
4

You don't need to use substr to remove the px. parseInt will do this for you:

topwithpx='0px';
var top = parseInt(topwithpx);
alert(top);  //alerts "0"

http://jsfiddle.net/kjMs9/3/

Comments

3

window.top is part of DOM 0 and can't be assigned a Number.

Avoid using global variables. Scope them with var

$(document).ready(function(){
    var topwithpx, topstr, top;
    topwithpx='0px';
    alert(topwithpx);
    topstr=topwithpx.substr(0,topwithpx.length-2);
    alert(topstr);
    top=parseInt(topstr);
    alert(top);
});​

2 Comments

This is about what I was going to post :) See this link please.
Thanks for any. My bad. Thats correct. +1 for quick ans to every one.
3

top is a default property of the window object (MDN). Name your variable something else.

2 Comments

@jAndy it's a reference to a window, not the current window. and yes, it is a property of the current window object.
I'm a little confused. top in window returns true in FF, but Object.getOwnPropertyNames( window ); doesn't show that property at all. Chrome on the other side, gets the top and self properties correctly with getOwnPropertyNames
2

top is a read-only property of the window (at least for Mozilla, but probably all the other big browsers as well).

Just change top to something else like topInt. Also, use var to declare variables (e.g. var topInt = parseInt(...). If you don't use var, then the window property is used by default, hence the read-only behavior.

By the way, it would be a bit nicer to use console.log instead of alert

1 Comment

1

top is a javascript window property. You can use top as a variable by doing this

var top = ...

2 Comments

top is not a reserved word.
@jbabey but it is a standard window property which you can't use for something else than reading, see this page.
1
$(document).ready(function(){
    topwithpx='0px';
    alert(topwithpx);
    topstr=topwithpx.substr(0,topwithpx.length-2);
    alert(topstr);
   var top=parseInt(topstr);
    alert(top);
});

you missing delcaration of variable

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.