Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bruce Gust

ASKER

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?
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>
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!