Bruce Gust
asked on
I need a script that reloads the page one time if "#" is present in the URL
I'm using this:
...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?
<!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>
...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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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>
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>
ASKER
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!
Thanks for the feedback!
ASKER
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?