kezkankrayon
01-28-2008, 09:54 AM
Am having trouble getting ajaxed content to scroll to a desired <div> identified by an id.
The code i have thus far will load new content into a div identified as 'scrolling_div' and it will even scroll 'scrolling_div' to a desired <div> within. However it will only scroll scrolling_div when a href is clicked for a second time. I would like to have the content load and then also have it scroll with the one click.
I'm stumped
here is the code -->
<script type="text/javascript">
function loadThenScroll(page,id){
getPage(page);
scrollDiv(id);
}
<!-- ajax -->
function getPage(page){
var xmlhttp=false; //Clear our fetching variable
try {
xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object…
} catch (e) {
try {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
} catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest(); //Starts an XMLHttpRequest if able to get a working active x object
}
var file = 'policies.php?category='; //This is the path to the file with data
xmlhttp.open('GET', file + page, true); //Open the file through GET, and add the page wanted to be retrieve as a GET variable
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) { //Check if it is ready to recieve data
var scrolling_div = xmlhttp.responseText; //The content data which has been retrieved
if( scrolling_div ){ //Makes sure there is something in the content variable (scroll_div)
document.getElementById('scrolling_div').innerHTML = scrolling_div; //Change the inner content of div to the newly retrieved content
}
}
}
xmlhttp.send(null) //Nullify the XMLHttpRequest
return;
}
<!-- scrolling function and variables -->
scrollSteps = 10
timer=""
moz=document.getElementById&&!document.all
function scrollDiv(id){
clearTimeout(timer)
scrollingDiv=document.getElementById("scrolling_div")
if((moz||window.opera)&&!scrollingDiv.style.position){
browserOffset=scrollingDiv.offsetTop
}
else{
browserOffset=0
}
if(scrollingDiv.scrollTop <= document.getElementById(id).offsetTop-scrollSteps-browserOffset){
scrollingDiv.scrollTop=document.getElementById("scrolling_div").scrollTop+scrollSteps
timer=setTimeout("scrollDiv('"+id+"')",10)
// if bottom of page reached before anchor point
if(scrollingDiv.scrollTop>(scrollingDiv.scrollHeight-scrollingDiv.offsetHeight)-scrollSteps){
clearTimeout(timer)
scrollingDiv.scrollTop=document.getElementById("scrolling_div").scrollHeight-scrollingDiv.offsetHeight
}
}
else{
if(scrollingDiv.scrollTop >= document.getElementById(id).offsetTop+scrollSteps-browserOffset){
scrollingDiv.scrollTop=document.getElementById("scrolling_div").scrollTop-scrollSteps
timer=setTimeout("scrollDiv('"+id+"')",10)
}
else{
clearTimeout(timer)
scrollingDiv.scrollTop=document.getElementById(id).offsetTop-browserOffset
}
}
}
function toTop(){
scrollingDiv=document.getElementById("scrolling_div")
clearTimeout(timer)
if(scrollingDiv.scrollTop >= scrollSteps){
scrollingDiv.scrollTop=document.getElementById("scrolling_div").scrollTop-scrollSteps
timer=setTimeout("toTop()",10)
}
else{
clearTimeout(timer)
scrollingDiv.scrollTop=0
}
}
</script>
</head>
<body>
<div id="scrolling_div" style="width:500px; height:100px;overflow:auto">
<div id="idWhatever">
text text text ...
</div>
<div id="idWhatever">
text text text ...
</div>
</div>
I have tried calling these two functions in different ways, but seem to be getting the same result. it will scroll the new content on the second click.
This is what i've tried.
<a href="#" onclick="getPage('pageWhatever'),scrollDiv('idWhatever')">someTitle</a>
<a href="#" onclick="loadThenScroll('pageWhatever','idWhatever')">someTitle</a>
<a href="javascript:getPage('pageWhatever'),scrollDiv('idWhatever')">someTitle</a>
<a href="javascript:loadThenScroll('pageWhatever','idWhatever')">someTitle</a>
I have even tried using different events to call the functions. This one did work, but it was dependent on how long the mouse button is held.
<a href="#" onmousedown="getPage('pageWhatever')" onmouseup="scrollDiv('idWhatever')">someTitle</a>
It seems that the scroll function starts before the ajax content has settled. Does anyone have any ideas?
any suggestions will be greatly appreciated
many thanks in advance
The code i have thus far will load new content into a div identified as 'scrolling_div' and it will even scroll 'scrolling_div' to a desired <div> within. However it will only scroll scrolling_div when a href is clicked for a second time. I would like to have the content load and then also have it scroll with the one click.
I'm stumped
here is the code -->
<script type="text/javascript">
function loadThenScroll(page,id){
getPage(page);
scrollDiv(id);
}
<!-- ajax -->
function getPage(page){
var xmlhttp=false; //Clear our fetching variable
try {
xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object…
} catch (e) {
try {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
} catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest(); //Starts an XMLHttpRequest if able to get a working active x object
}
var file = 'policies.php?category='; //This is the path to the file with data
xmlhttp.open('GET', file + page, true); //Open the file through GET, and add the page wanted to be retrieve as a GET variable
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) { //Check if it is ready to recieve data
var scrolling_div = xmlhttp.responseText; //The content data which has been retrieved
if( scrolling_div ){ //Makes sure there is something in the content variable (scroll_div)
document.getElementById('scrolling_div').innerHTML = scrolling_div; //Change the inner content of div to the newly retrieved content
}
}
}
xmlhttp.send(null) //Nullify the XMLHttpRequest
return;
}
<!-- scrolling function and variables -->
scrollSteps = 10
timer=""
moz=document.getElementById&&!document.all
function scrollDiv(id){
clearTimeout(timer)
scrollingDiv=document.getElementById("scrolling_div")
if((moz||window.opera)&&!scrollingDiv.style.position){
browserOffset=scrollingDiv.offsetTop
}
else{
browserOffset=0
}
if(scrollingDiv.scrollTop <= document.getElementById(id).offsetTop-scrollSteps-browserOffset){
scrollingDiv.scrollTop=document.getElementById("scrolling_div").scrollTop+scrollSteps
timer=setTimeout("scrollDiv('"+id+"')",10)
// if bottom of page reached before anchor point
if(scrollingDiv.scrollTop>(scrollingDiv.scrollHeight-scrollingDiv.offsetHeight)-scrollSteps){
clearTimeout(timer)
scrollingDiv.scrollTop=document.getElementById("scrolling_div").scrollHeight-scrollingDiv.offsetHeight
}
}
else{
if(scrollingDiv.scrollTop >= document.getElementById(id).offsetTop+scrollSteps-browserOffset){
scrollingDiv.scrollTop=document.getElementById("scrolling_div").scrollTop-scrollSteps
timer=setTimeout("scrollDiv('"+id+"')",10)
}
else{
clearTimeout(timer)
scrollingDiv.scrollTop=document.getElementById(id).offsetTop-browserOffset
}
}
}
function toTop(){
scrollingDiv=document.getElementById("scrolling_div")
clearTimeout(timer)
if(scrollingDiv.scrollTop >= scrollSteps){
scrollingDiv.scrollTop=document.getElementById("scrolling_div").scrollTop-scrollSteps
timer=setTimeout("toTop()",10)
}
else{
clearTimeout(timer)
scrollingDiv.scrollTop=0
}
}
</script>
</head>
<body>
<div id="scrolling_div" style="width:500px; height:100px;overflow:auto">
<div id="idWhatever">
text text text ...
</div>
<div id="idWhatever">
text text text ...
</div>
</div>
I have tried calling these two functions in different ways, but seem to be getting the same result. it will scroll the new content on the second click.
This is what i've tried.
<a href="#" onclick="getPage('pageWhatever'),scrollDiv('idWhatever')">someTitle</a>
<a href="#" onclick="loadThenScroll('pageWhatever','idWhatever')">someTitle</a>
<a href="javascript:getPage('pageWhatever'),scrollDiv('idWhatever')">someTitle</a>
<a href="javascript:loadThenScroll('pageWhatever','idWhatever')">someTitle</a>
I have even tried using different events to call the functions. This one did work, but it was dependent on how long the mouse button is held.
<a href="#" onmousedown="getPage('pageWhatever')" onmouseup="scrollDiv('idWhatever')">someTitle</a>
It seems that the scroll function starts before the ajax content has settled. Does anyone have any ideas?
any suggestions will be greatly appreciated
many thanks in advance