Archive

Archive for the ‘Javascript’ Category

Leaflink Explaind – Part 2b (Save scroll position)

May 18th, 2005 ramin View Comments

In part 2a of our tutorial we went over the Dashboard, how it was created and what it was used for. You do not need to read and/or understand part 2a to read part 2b. They are totally unrelated, save for the fact that I am using them both on the same project.

In this tutorial I will be discussing the technique used to save the current scroll position of the page when submitting a form and returning to the same page. In some cases, it is desirable that you maintain the scroll position when returning to the same page after a form submittal. This tutorial is a lot shorter and probably easier to implement and understand.

The basic concept is this:

  • using javascript, we get the current X and Y scroll positions of the page.
  • set those values to hidden fields within our form.
  • submit the form, which now contains the X and Y scroll values.
  • perform server-side logic.
  • this is the most important part: when redirecting back to our original page, we need to pass back the X and Y values as querystring variables. Otherwise, we have no idea where to scroll to.
  • at the bottom of the page or using onload, we get the X and Y values and using some more javascript, we scroll to the original position we were at before the page was submitted.

That's it really. Usually this happens very fast, especially if most of the content on the page has already been downloaded and cached by the browser. You barely ever notice the sudden jerk or jump as the page jumps to the previous scroll position. In my tests, it almost appeared as if I never left the page at all.

Example Link

Here is the javascript funtion I use to save the scroll position of the page right before I submit the page. This function can either be called onsubmit or within a custom function which actually submits a form for you.

function saveScrollPositions(theForm) {
	if(theForm) {
		var scrolly = typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement.scrollTop;
		var scrollx = typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement.scrollLeft;

		theForm.scrollx.value = scrollx;
		theForm.scrolly.value = scrolly;
	}
}

Note that I have not tested this on many browsers, but it works fine in IE 6.x and FF. I'm assuming it'll work in IE 5.5 and most other mozilla browsers.

Now on the server-side we do all the necessary logic and when it is time to redirect back to the original page, we simply pass back the scrollx and scrolly values as part of the query string. Since I was using ASP for the Leaflink project, this is how it looked:

scrollx = Request.QueryString("scrollx")
scrolly = Request.QueryString("scrolly")
' redirect back to original page (index.asp in this case)
' with scrollx and scrolly passed back as query string variables
Response.Redirect "/leaflink/?scrollx=" & scrollx & "&scrolly=" & scrolly

Once we're back on the originating page, we simply grab the scrollx and scrolly values from the query string and scroll to their position(s).

// use this to scroll to the previous scroll position
// when form was submitted.
window.scrollTo(< %= Request.QueryString("scrollx") %>, < %= Request.QueryString("scrolly") %>);

PHP version of the above code, at the bottom of our page:

$scrollx = 0;
$scrolly = 0;

if(!empty($_REQUEST['scrollx'])) {
    $scrollx = $_REQUEST['scrollx'];
}

if(!empty($_REQUEST['scrolly'])) {
    $scrolly = $_REQUEST['scrolly'];
}

// use this to scroll to the previous scroll position
// when form was submitted.
window.scrollTo(< ?= $scrollx ?>, < ?= $scrolly ?>);

I put that last bit of code at the bottom of my page, but it could've been put inside an onload event handler as well. Either one would work just fine.

That's it for this tutorial. Hope you find it useful. If you have any questions and/or comments, feel free to contact me.

Categories: Javascript, Tutorials Tags:

TILT: The Table-less Table Layout

May 4th, 2005 ramin View Comments

Here is javascript component that allows you to dynamically insert tables into your page w/o actually putting any TABLE tags in your code. The technique uses the DOM and inserts the table using existing data on the page. This is a nice alternative to purely CSS driven layouts vs. table layouts.

Home of the author of TILT:

http://glazkov.com/blog/archive/2005/05/02/476.aspx

[via thejavascriptweblog]

Categories: Javascript, Resources, xHTML / CSS Tags:

Web Development Bookmarklets

April 23rd, 2005 ramin View Comments

For all you web developers out there, here is a collection of bookmarklets that you can use in your mozilla browser to help you debug your sites. If you are truly serious about web development, then you need to either get the Web Developer Extension, or use the bookmarklets mentioned on this site:

http://www.squarefree.com/bookmarklets/webdevel.html

Those are, of couse, just the web developer bookmarklets. You can view the rest of them here:

http://www.squarefree.com/bookmarklets/

You may also download and install ALL of the bookmarklets using the Bookmark import tool. Follow the instructions on this page to find out how:

http://www.squarefree.com/bookmarklets/importall.html

Categories: Javascript Tags:

Javascript Enabled Stylesheets

April 14th, 2005 ramin View Comments

Are your sites heavily dependant on javascript? Yeah, so are mine. Sometimes its just not worth the trouble for me to make my sites work for the 1-2% of the population who might have javascript turned off. Just about every browser has javascript support, so even if they do have it turned off, they can easily turn it on.

But in any case, if you really care about compliancy and accessibility and want to have more control over your page w/o javascript, then check out this link:

Javascript Enabled Stylesheets

It shows you how to include a certain stylesheet only if JS is enabled. This is handy in those cases where you have sections of your site hidden by default and use JS to show them.

Categories: Javascript Tags:

More Nifty Corners (Nifty Corners 2)

April 12th, 2005 ramin View Comments

A few weeks ago I found out about Nifty Corners, a javascript/css based technique of creating rounded corners without using any images at all. The library was pretty impressive and worked very nicely. It didn't require you to add any extra markup to your html either. All you had to do was define which elements you wanted to make rounded, specify the corner colors as well as the background color and it did the rest.

Well since then the original author of the library has been improving on it and adding new features. This new version is even more impressive than the previous one. It allows you to define exactly which corners you want rounded (tl, tr, bl, br or a combination). It also has a new feature that smoothes out the corners using some slight anti-aliasing. I haven't digged into the code to see how the aa'ing is done, but it works! Very nice.

http://pro.html.it/articoli/id_599/idcat_31/pro.html

Or for the impatient, links that go directly to the examples:

  1. Example one: a single div with antialias
  2. Example two: brothers divs
  3. Example three: transparent by nature
  4. Example four: with or without them
  5. Example five: color explosion
  6. Example six: elastic nifty
  7. Example seven: transparent, tabbed menu. Now with borders.
  8. Example eight: navigational leaves
  9. Example nine: a floated image gallery
  10. Example ten: Nifty layout

And here is the link to download the library: nifty2.zip

Categories: Javascript, xHTML / CSS Tags: