CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2006
    Posts
    10

    To Get the Scroll Height

    Hi,

    In aspx page

    i wrote one function in javasript to show the scrolling height

    function scrollingDetector(){

    if (navigator.appName == "Microsoft Internet Explorer"){

    alert("You've scrolled to " + document.body.scrollTop + " pixels.");

    }else{alert ("You've scrolled to " + window.pageYOffset + " pixels.");}


    }

    and calling this function in <body onscroll= scrollingDetector()

    this one is working fine if am not including this below line in the top of <html>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    but if i include this line onscroll property of body is not working.

    Anybody please help me why it is happening..

    waiting for your valuable response

    Veeres

  2. #2
    Join Date
    Jul 2007
    Location
    Sweden
    Posts
    331

    Re: To Get the Scroll Height

    Relying on the exact browser like that is bad because you will never get good cross-browser compatability. Here's a script taken from www.quirksmode.org, a great resource on cross-browser compatible JavaScript:
    Code:
    var x,y;
    if (self.pageYOffset) // all except Explorer
    {
    	x = self.pageXOffset;
    	y = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
    	// Explorer 6 Strict
    {
    	x = document.documentElement.scrollLeft;
    	y = document.documentElement.scrollTop;
    }
    else if (document.body) // all other Explorers
    {
    	x = document.body.scrollLeft;
    	y = document.body.scrollTop;
    }

  3. #3
    Join Date
    May 2002
    Posts
    10,943

    Re: To Get the Scroll Height

    EDIT: Misunderstood the question.
    Last edited by PeejAvery; September 21st, 2007 at 11:27 AM.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  4. #4
    Join Date
    Jul 2007
    Location
    Sweden
    Posts
    331

    Re: To Get the Scroll Height

    The code I posted should work in all browsers. Yours doesn't retrieve the values he wanted (he wanted the position of the scroll, not the dimensions of the page.)

    The problem he was experiencing was that once he adds the document type, IE goes into strict mode at which point you have to use document.documentElement instead of document.body.

    Also, I'm pretty sure that code you posted comes from Quirksmode as well...
    Last edited by andreasblixt; September 21st, 2007 at 11:14 AM.

  5. #5
    Join Date
    May 2002
    Posts
    10,943

    Re: To Get the Scroll Height

    Quote Originally Posted by andreasblixt
    Also, I'm pretty sure that code you posted comes from Quirksmode as well...
    Really? Where? I wrote it a while back when working with an overlay script.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured