JS jump to anchor

I have a search results page that I need to jump to an anchor called "results" when it loads.  I cannot use the #results in the URL, and I cannot use a body onload func.  it needs to be a standalone func with the anchor "results" built in.
LVL 1
bjv211Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RoonaanCommented:
Do you have a form-field (textfield/radio/checkbox/textarea) somewhere near #results? Then you could use: document.forms.formName.fieldName.focus();

-r-
bjv211Author Commented:
ok so if i put one in;  <form name="resultsanchor"><input type="text" name="results"></form>  how would i use this keeping in my my original criteria.
RoonaanCommented:
<script type="text/javascript">
setTimeout("document.forms.resultsanchor.results.focus()",10);
</script>

-r-
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Samuel LiewCommented:
You don't need a form and a text field.

Solution:

1) Put this script into the head of the page:
<script type="text/javascript"><!--
function findPosX(obj) {
      var curleft = 0;
      if (obj.offsetParent) {
            while (obj.offsetParent) {
                  curleft += obj.offsetLeft
                  obj = obj.offsetParent;
            }
      }
      else if (obj.x)
            curleft += obj.x;
      return curleft;
}
function findPosY(obj) {
      var curtop = 0;
      if (obj.offsetParent) {
            while (obj.offsetParent) {
                  curtop += obj.offsetTop
                  obj = obj.offsetParent;
            }
      }
      else if (obj.y)
            curtop += obj.y;
      return curtop;
}
function goto(objID) {
      window.scrollTo(findPosX(document.getElementById(objID)),findPosY(document.getElementById(objID)));
}
//--></script>

2) Modify the body onload function:
<body onload="goto('bookmark');">

3) Insert the bookmark wherever you want:
<a id="bookmark"></a>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bjv211Author Commented:
quoted from above  >>>and I cannot use a body onload func.
could i somehow use the bookmark varible in the function so it would automatically do it when the page loads rather than call it in the body tag.
bjv211Author Commented:
Roonaan, this doesn't mimic an anchor, it simply switches focus to the form field.
bjv211Author Commented:
sam2912, i got a workaround for the body onload tag, it works great!! thanks :)
Samuel LiewCommented:
You also can use the id attribute on any tag, and the script will jump to it.

If you don't want onload, you can paste this just before the body closing tag </body> :
<script type="text/javascript"><!--
goto("bookmark");
//--></script>
Samuel LiewCommented:
Thanks for the points, and have a nice day!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.