![]() |
Web Development »
Client side scripting »
General
Intermediate
License: The Code Project Open License (CPOL)
Scroll To previous focus element on the pageBy senthil karuppaiahScroll To previous focus element on the page when the same page returned from the server. |
Javascript, Windows, Visual Studio, Dev
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
When the same page returned from the server, scroll to previous focus element on the page. Example: If selecting a value from the dropdown placed way below on the page refresh the page, by default first field will receive the focus or scroll position set to Zero. It forces the user to scroll down to the previous position and lot of frustration.
With simple hidden fields, we can come back to same position and set the focus on same field or next field. This will allow the user to continue from the place where they left off.
Add Two Hidden fields under the FORM tag.
<input type="hidden" name = "previousElement"> <input type="hidden" name = "previousScrollPosition">
Add these javascripts under script tag or in your .js file.
function sendScrollPosition() {
document.forms[0].elements["previousScrollPosition"].value = document.body.scrollTop;
return true;
}
function setfocuselement(ele){
if(document.forms[0] == null) return false;
if(document.forms[0].elements["previousElement"] != null) {
document.forms[0].elements["previousElement"].value=ele.name || event.srcElement.name;
}
}
if(document.getElementsByName("previousScrollPosition").length > 0) {
document.body.scrollTop = document.forms[0].elements["previousScrollPosition"].value;
}
var prvElement= document.forms[0].elements["previousElement"];
if(prvElement != null && prvElement.value != null ) {
document.forms[0].elements[prvElement.value].focus();
}
When you submit the page, you need to call those two javascripts. You can add those javascript function to onchange or onblur or onclick events and finaly submit the page to server.
When you render the ASP or JSP page, you need to take both previousScrollPosition, previousElement value from the Request object and populate both the hidden fields.
<form action="demo_form2.asp" method="post"> First name: <input type="text" name="name" value="Donald" /> <br /> Last name: <input type="text" name="name" value="Duck" /> <br /> <input type="hidden" name = "previousElement" value="<%Request.Form("previousElement")%>"> <input type="hidden" name = "previousScrollPosition" value="<%Request.Form("previousScrollPosition")%>"> <input type="submit" value="Submit" /> </form>
You need to make sure that form fields available in the FORM before calling or using the javascript.
If you have multiple fields with same name, then you need to send the index of the field to the server and get the field list using document.getElementsbyName("elementName") function and loop through them to select correct index and set the focus.
Setting focus on next element or skiping hidden or disabled fields while doing setfocus is a complex javascript. I will show it in different article.
| You must Sign In to use this message board. | |||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 19 Aug 2006 Editor: Sean Ewington |
Copyright 2006 by senthil karuppaiah Everything else Copyright © CodeProject, 1999-2009 Web21 | Advertise on the Code Project |