Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWeb Page and Site Features

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

On-the-Fly 'Scroll-To' Anchor

Recently, we had a situation where certain incoming links to a page needed to scroll down automatically. The incoming link URLs didn't have #name appended (we'll call it a "scroll-to" anchor).

(The HTML name/id anchor article contains both the old and the new way of specifying and marking scroll-to anchors.)

To resolve the situation, an automatic scroll-to needed to be implemented so arrivals would immediately see the pertinent information they were looking for — instead of assuming the information isn't there and moving on.

Not all incoming traffic needed to scroll down to the certain point. Only the ones with a specific ?... URL parameter. Example:

http://example.com/page.html?A=B

We needed to somehow make the browser act as if #name was appended to the link URL, like:

http://example.com/page.html?A=B#name

But only when the URL contained the ?A=B parameter and no scroll-to anchor (#name).

A fairly simple little JavaScript was implemented. It causes the browser to act as if the link URL actually had #name appended to it.

The JavaScript has an if() function to test whether or not the link URL contains the ?A=B parameter. Because only those should have a scroll-to anchor appended.

Then there's a second if() function to test whether or not the link URL already has a scroll-to anchor. Only if it doesn't should #name be appended to the link URL.

Here's the JavaScript. Notes follow.

<script>
if( location.search && location.search.match(/^\?A=B/) )
{
   if( (!location.hash) || location.hash.length == 0 )
   {
      location.hash = "name";
   }
}
</script>

There are two places to customize.

  1. The URL parameter colored blue needs to be changed to the parameter you're testing for. If the parameter value (the part after the "=" character) is changeable, then don't specify anything following the "=" character.

  2. The scroll-to anchor colored purple needs to be changed to the scroll-to anchor name on your page where the browser needs to scroll to.

During the loading process, the JavaScript will run as soon as the browser encounters it.

Therefore, put the JavaScript in the source code somewhere below the place where the anchor name is located. And also below the place that would be at the bottom of the browser window when the page has finished scrolling.

The reason to put the JavaScript below the place where the anchor name is located is so the JavaScript can find it.

The reason to put the JavaScript below the place that would be at the bottom of the browser window when the page has finished scrolling is that the page can't scroll any further when the JavaScript runs. Because the JavaScript runs as soon as it's encountered, and the browser creates the page as it goes along, the JavaScript runs before the rest of the page exists.

The JavaScript is fairly easy to implement. Use it whenever you need to programmatically insert a hash name in the link URL of arriving browsers.

(This article first appeared in Possibilities ezine.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List
software index page

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC