Welcome Guest!
Please login
If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:
- Receive Fast Advice
- Learn Programming Languages
- Get Professional Website Reviews
- Quick Troubleshooting Assistance
|
|
Measuring With Javascript?
This is a discussion on Measuring With Javascript?, within the Javascript section. This forum and the thread "Measuring With Javascript?" are both part of the Programming Your Website category.
![]() ![]() |
Apr 14 2008, 07:16 AM
Post
#1
|
|
|
Master of the Universe ![]() ![]() ![]() ![]() ![]() ![]() Group: Advisors Posts: 1,157 Joined: 15-February 08 From: London, England Member No.: 141 |
Is it possible to detect the height of an element? I am looking for a way to measure the height of some dynamic content.
Furthermore. How do I create one of those nifty floating javascript boxes? If the idea in my head works I will post probably the coolest link you will see all day -------------------- |
|
|
Apr 14 2008, 07:45 AM
Post
#2
|
|
|
Squeeze Machine ![]() ![]() ![]() ![]() ![]() Group: Mentor Posts: 601 Joined: 15-February 08 From: UK Member No.: 143 |
Is it possible to detect the height of an element? I am looking for a way to measure the height of some dynamic content. Furthermore. How do I create one of those nifty floating javascript boxes? If the idea in my head works I will post probably the coolest link you will see all day Have you tried just accessing the CSS style? CODE var x = document.getElementByID("myElement").style.height By "nifty floating boxes", do you mean this sort of thing (a good read, by the way)? |
|
|
Apr 14 2008, 09:24 AM
Post
#3
|
|
![]() Rapid Squeezer ![]() ![]() ![]() ![]() Group: Members Posts: 123 Joined: 14-February 08 From: Hounslow, London Member No.: 133 |
I may be wrong, but I seem to remember that styles can't be read using JavaScript.
-------------------- |
|
|
Apr 14 2008, 09:39 AM
Post
#4
|
|
|
Squeeze Machine ![]() ![]() ![]() ![]() ![]() Group: Mentor Posts: 601 Joined: 15-February 08 From: UK Member No.: 143 |
|
|
|
Apr 14 2008, 03:35 PM
Post
#5
|
|
![]() Squeeze Machine ![]() ![]() ![]() ![]() ![]() Group: Administrators Posts: 643 Joined: 13-February 08 From: Catching the squeezed drips downunder. Member No.: 13 |
Hi Jason,
Styles can definitely be read using javascript but only if they are set in the first place either via CSS or javascript. Due to the units of measurement, CSS styles are usually treated as strings so you would alter Mike's snippet a touch to: CODE var x = parseInt(document.getElementById('element').style.height); However, if there is no style height attribute set via CSS or Javascript this will yield either null, 0 or NaN. The other option -- which will have slightly different results on each browser -- is CODE var x = document.getElementById('element').offsetHeight; The fact that each browser returns slightly different values usually doesn't matter a great deal because the values will only be used within that same browser. Hope that helps. Cheers, Rakuli. -------------------- |
|
|
Apr 14 2008, 04:08 PM
Post
#6
|
|
![]() Squeeze Machine ![]() ![]() ![]() ![]() ![]() Group: Administrators Posts: 643 Joined: 13-February 08 From: Catching the squeezed drips downunder. Member No.: 13 |
I just read the second part of your question.
I have coded those 'nifty floating boxes' many times and I have never had to know the height of a box to do so. The best way to do this manipulating the top and left CSS values of absolute or relatively positioned elements. If, for example, the top and left properties have not been set with CSS or Javascript you can find it's position on the page using CODE var x = document.getElementById('element').offsetLeft; var y = document.getElementById('element').offsetTop; As above, this will yield slightly different results in each browser but as you are using it to find a starting position only, you need not worry about this variance once the 'nifty floating' begins -------------------- |
|
|
Apr 14 2008, 04:55 PM
Post
#7
|
|
|
Squeeze Machine ![]() ![]() ![]() ![]() ![]() Group: Mentor Posts: 601 Joined: 15-February 08 From: UK Member No.: 143 |
Styles can definitely be read using javascript but only if they are set in the first place either via CSS or javascript. Due to the units of measurement, CSS styles are usually treated as strings so you would alter Mike's snippet a touch to: CODE var x = parseInt(document.getElementById('element').style.height); Ooooh, interesting. I have a strong sense of deja-vu right now. It's as though you already taught me this months ago. |
|
|
Apr 14 2008, 05:08 PM
Post
#8
|
|
![]() Squeeze Machine ![]() ![]() ![]() ![]() ![]() Group: Administrators Posts: 643 Joined: 13-February 08 From: Catching the squeezed drips downunder. Member No.: 13 |
Ha! I'm not sure about that Mike -- I think you're giving me credit for a lesson I didn't teach there
One thing I should point out as well when reading CSS styles is that it will return the integer value for the measurement unit you have used. When dealing with floating elements in JavaScript it is rare you will want anything apart from the pixel value (percentages on occasion). If you have an elastic layout using em's or ex's, this value will be of little use so you will want to use the offsetXXXX methods to get a pixel representation. -------------------- |
|
|
Apr 15 2008, 02:27 AM
Post
#9
|
|
|
Master of the Universe ![]() ![]() ![]() ![]() ![]() ![]() Group: Advisors Posts: 1,157 Joined: 15-February 08 From: London, England Member No.: 141 |
Alright, basically what I am trying to do is detect people running adblock plus. My site uses banner ads sparingly and in unobtrusive places. These ads pay for my websites hosting (or will do one day). In my terms it will say that the ads must be displayed to use my site.
I should note this is an experiment. The idea being that I know the height of a Google banner ad is either 60 or 75 pixels. If I can measure the height of the ad div (which is unstyled) or the height between the content and bottom of my page, I will be able to work out if an advert is beings displayed or not. If an ad doesn't display after a set amount of time the user will get an x marked in their cookie. If they get 3 or 5 of these a pop up message will say. 'I suspect you are using some ad blocking software, please white list this site to continue browsing.' If they continue to browse with ad blocking software I will simply ip ban them. - The time delay will be there to allow ads to load for dial up users. - The 3-5 marks is to allow for the once in a blue moon when an ad may fail to load. - The warning is to give ABP users a chance. Visitors using ABP will not realize anything is being blocked. This will let them unblock the site. - The IP ban is to remove people who I have absolutely no chance of gaining revenue from. I will let you know if it works. -------------------- |
|
|
Apr 15 2008, 04:49 AM
Post
#10
|
|
|
Squeeze Machine ![]() ![]() ![]() ![]() ![]() Group: Mentor Posts: 601 Joined: 15-February 08 From: UK Member No.: 143 |
Alright, basically what I am trying to do is detect people running adblock plus. My site uses banner ads sparingly and in unobtrusive places. These ads pay for my websites hosting (or will do one day). In my terms it will say that the ads must be displayed to use my site. What a lot of work, for so little reward! You'd be better off spending this time developing your content, and experimenting with other revenue streams. What's the point of concentrating on the small stuff? My hosting only costs $2 a month. Assuming yours is similar, why would you care about such small sums of money? Think about it in terms of the value of your time: how long will it take to do this? How much is your time worth? If I work for one hour, then I've earned enough money to cover my web hosting for 2 years. So why bother spending hours to make a tiny increase in income/bandwidth ratios? It seems to me that you're motivated by control-freakery, not by genuine financial concerns. This post has been edited by MikeHopley: Apr 15 2008, 05:09 AM |
|
|
Apr 15 2008, 05:28 AM
Post
#11
|
|
|
Master of the Universe ![]() ![]() ![]() ![]() ![]() ![]() Group: Advisors Posts: 1,157 Joined: 15-February 08 From: London, England Member No.: 141 |
What a lot of work, for so little reward! Achieving something I haven't done before is just as good as financial rewards. QUOTE It seems to me that you're motivated by control-freakery, not by genuine financial concerns. This is my personal site and to be honest I will do what I want with it. I make my money from web design. My personal site contains things that I cannot do with my portfolio. This includes doing time consuming things which will affect only a small number of visitors. I do these things because they are interesting and challenging. The reward isn't financial, its personal. I got the idea after reading the following article and some others with the same topic. http://blogs.guardian.co.uk/technology/arc...ad_updated.html Ad Block Plus is a nice plug-in. Really it is. It kills off flash ads which make unwanted noise. At the same time the adverts are there for a reason and by blocking them all you are doing is denying a site of potential revenue. I am not casting a judgement on people who choose to use it. All I am trying to do is create a system to find these users and ask them to whitelist the website they are viewing. There are several ways to achieve this. I have gone for the most obvious way by detecting if the space for the ad is actually being filled up. Whether it works or not is another thing. -------------------- |
|
|
Apr 15 2008, 06:11 AM
Post
#12
|
|
|
Squeeze Machine ![]() ![]() ![]() ![]() ![]() Group: Mentor Posts: 601 Joined: 15-February 08 From: UK Member No.: 143 |
QUOTE This is my personal site and to be honest I will do what I want with it. Well, obviously. QUOTE This includes doing time consuming things which will affect only a small number of visitors. I do these things because they are interesting and challenging. The reward isn't financial, its personal. Whatever floats your boat. Seems pretty dull to me, but we're all different. But in any case I'm sure you will learn some useful skills in the process of doing this useless thing. This post has been edited by MikeHopley: Apr 15 2008, 06:11 AM |
|
|
Apr 15 2008, 10:36 AM
Post
#13
|
|
![]() Rapid Squeezer ![]() ![]() ![]() ![]() Group: Advisors Posts: 176 Joined: 14-February 08 From: Willich, Germany Member No.: 56 |
I am not casting a judgement on people who choose to use it. All I am trying to do is create a system to find these users and ask them to whitelist the website they are viewing. Why would I do that? The reason I use ABP is because I DON't want to see any ads. AT ALL! I don't even see the ads here on TWS... not even the cute little "squeeze squares"... I'm not about to go white-listing your site, just so you can show me your stupid ads... In my opinion this is Usability -101.... -------------------- www.c010depunkk.com ~ the hangout of a web developer
|
|
|
Apr 17 2008, 12:55 AM
Post
#14
|
|
![]() Rapid Squeezer ![]() ![]() ![]() ![]() Group: Members Posts: 123 Joined: 14-February 08 From: Hounslow, London Member No.: 133 |
Thanks Rakuli, that will come in handy sometime
-------------------- |
|
|
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
![]() ![]() |
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
Similar Topics
| Topic Title | Replies | Topic Starter | Views | Last Action | |||
|---|---|---|---|---|---|---|---|
![]() |
2 | marSoul | 245 | 23rd February 2008 - 10:52 AM Last post by: marSoul |
|||
![]() |
2 | Jason | 143 | 3rd April 2008 - 05:57 AM Last post by: Jason |
|||
![]() |
6 | Jason | 243 | 2nd June 2008 - 04:35 AM Last post by: Monie |
|||
![]() |
2 | MikeHopley | 183 | 2nd November 2008 - 06:17 PM Last post by: MikeHopley |
|||
![]() |
6 | Squeeze Bot | 324 | 27th June 2008 - 09:55 AM Last post by: Rakuli |
|||






Apr 14 2008, 07:16 AM









