I need a script that reloads the page one time if "#" is present in the URL

I'm using this:

<!DOCTYPE html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function(){    
    if(document.URL.indexOf("#")==-1){
        // Set the URL to whatever it was plus "#".
        url = document.URL+"#";
        location = "#";

        //Reload the page
        location.reload(true);
    }
});
</script>
</head>
<body>
<br><br>Whatever<br><br><br><br><br><br><br><span id="33">hello</span>
</body>
</html>

Open in new window


...and it doesn't seem to work.

What's happening is I've got a search function that redirects the user to a page that's fairly lone. Embedded within the URL is a "#" followed by an id number that, theoretically, moves the page down to the precise location of the person they were searching for.

What I've got is inconsistent which has led me to Javascript, hoping that by using Javascript I can establish something consistent.

As an aside, I've been warned that "#" tags only work when you're on that page. That being the case, my "workaround" is to refresh the page as soon as it loads.

Which brings us to the code that I've attempted to use with no result. What am I doing wrong or is there a better solution?
Bruce GustPHP DeveloperAsked:
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.

Michel PlungjanIT ExpertCommented:
Hi

I think you have an XY problem.

I assume your question is as follows:

I need to scroll the page down to the element with ID=33

and the immediate solution is not to use script but to not have an ID beginning with a digit since that only works for HTML5 doctypes so EITHER add a letter to the beginning of the ID or change the doctype to

<!DOCTYPE html>

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
Bruce GustPHP DeveloperAuthor Commented:
Michel! Thanks for getting back with me! I implemented your suggestion, but the inconsistency persists and I think it's now even more obvious before that my dilemma is that the kind of anchor I'm using ("#") is effective only when you're on that page. In other words, I'm not guaranteed a consistent result if I'm attempting to route my user to a specific location on a page from another page, yes?

At least, that's what I've read as I've googled around.

If that is the case, then my initial approach to reloading the page is, once again, center stage. Do you have a suggestion as far as a script that will automatically reload the page one time when a user first accesses it?
Michel PlungjanIT ExpertCommented:
I have never heard of any inconsistencies

If you have UNIQUE IDs, a link like

http://yourserver.com/yourpage.html#x33 

will take you to

<span id="x33">Here</span>

in a modern browser. For backwards compatibility you can do this instead

<a name="x33">Here</a>
Bruce GustPHP DeveloperAuthor Commented:
I found an article that referenced the inconsistencies of anchor tags based on the rate at which the page loaded. The way in which the problem was described matched my scenario to the tee. Sure enough, once I streamlined my select statement and tried the anchor tag again, it worked perfectly.

Thanks for the feedback!
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.