Go To HTML anchor from 3rd party program

First post. Very little HTML experience. Moderator: If this is the wrong forum, please move.

The problem I’m running into is less of an HTML/Javascript problem and more of a “what is written for one web browser doesn’t necessarily work (or work the same) on other web browsers.”

Requirement (The short version)
Instruct web browser to “go to” a specific HTML anchor tag from a 3rd party program. Solution must also work after the page has been rendered.

Requirement/The Problem (The long version)
I’m trying to develop a feature for an application I’ve written. I call the new feature “Smart Navigation”. Not necessarily the final name. The way it should work is fairly simple. Document 1 (local, dynamic, non-HTML) has pointers to HTML anchor labels in Document 2 (local, dynamic, HTML). When the user specifies use of a pointer in document 1, the web browser should “go to” the related anchor in Document 2.

When opening the web browser and rendering the HTML document for the first time, navigation is fairly straightforward. I can simply add “#{AnchorName}” to the end of document name. However, once the web browser is open, there is no way to update the browser’s command line without writing code for every web browser out there or restricting the user to Internet Explorer or a to custom window that uses a Web Browser Object or another custom web object.

So far, the solutions I’ve come up with work on some (most?) major web browsers, but not all. Unfortunately, the “problem” web browser(s) always includes at least one major browser. These solutions work by opening the web browser on the first request and refreshing (sending the F5 key) on subsequent requests.

Solution 1: HTML “Meta Refresh” tag. This solution embeds instructions to refresh the document to a specific anchor location when the document is originally rendered and when the document is refreshed (F5). The code is embedded in the header and looks something like this:

    <meta http-equiv="refresh" content="0;url=#Line432">

This solution works flawlessly on all major web browsers except Opera (creates a perpetual reload loop) and Firefox and other Gecko derivatives (works fine for while but eventually goes into a reload loop).

Solution 2: Javascript “Location.Replace”. This solution is essentially the same as Solution 1 except that javascript is used. The code looks something like this:

    <script language="javascript">

This solution works flawless on some major web browsers but has problems with others. On Firefox and Gecko derivatives, it requires an additional refresh (F5) command to work. On Chrome and derivatives, it only works on the first call. i.e. F5 does nothing. On Opera, it works for a while and then it just stops working.

The feature is not a show stopper but it would be nice to figure out a way to “go to” a specific location in an HTML document without having to open a new window for each request or writing unique code for a few browsers.

If you have any ideas, let me know. Thanks. :slight_smile:

If you put the script in the HEAD it runs BEFORE the document is fully built, so everything in BODY shouldn’t exist yet making your scripting bomb.

Place it right before </body> near the end of the document and you might have better results. You may have to ‘hook’ it with onload though for it to really work.

Though really said ‘third party program’ should at least be able to add #wherever to the end of the linking url’s.

Thank you for your prompt response felgall. 12 minutes. That’s fast!

I think you might have misunderstood my requirement/problem a bit. I’ll try to clarify. Any misunderstandings are my fault for not being clear. I apologize.

I’m not having any problems going to the requested label on the first request. If I just needed to go to one label one time, I would add the #id to the end of the URL with the page is first rendered. The problem is going to other labels after the page has been rendered via instructions provided by a third party program. Think of the web browser (in this scenario) as a viewer only. The user reads from it but never touches/clicks on it.

A lot of stuff. A few misunderstandings (definitely my fault) but also some good stuff.

Document 1 is not an HTML document. It is not even in a web browser. It is a document in a 3rd-party program. I only mentioned it because Document 1 contains pointers to anchor labels in Document 2.

I looked at the location.hash = ‘id’ statement that you mentioned with great hope but I couldn’t get it to work. I have very limited HTML/Javascript experience so I’m sure I did something wrong.

If the location.hash = ‘id’ statement is executed after the document has rendered (new or refresh) as part of a OnClick or similar command, it works great. But and however, executing this command while the document is rendering (new or refresh), the command is effectively ignored.

Is there any way to execute this command automatically but waiting until after the document has rendered (sleep, timer, etc.)? More generically, is there any command (HTML, Javascript, anything) that will automatically “go to” to a specific anchor label when the document is refreshed (F5)?

Thank you for your assistance. I appreciate it!

No they are not the same thing at all. Internet Explorer clears any form fields when you do a refresh whereas browsers that do a reload generally retain any changes you may have made to form content. As far as I have been able to determine browsers that do a reload all handle things exactly the same way.

As long as all of them reference the full address of the page before the # you shouldn’t have any problems with them all being able to jump straight to the right place in the page.

It is only where you have references within the same page to a part of the page that hasn’t loaded yet that it will not be able to find where to jump to.

Where the page positions itself when you refresh the page depends on the browser (particularly if it is done using F5 which some browsers use for reload instead of refresh).

If your “third party” is JavaScript then simply attaching it to the bottom of the page and updating location.hash as required should jump the page to the appropriate ids.

Simply adding the #id to the end of the URL will jump to that particular id in the page you are going to in all modern browsers. There shouldn’t be any need to update anything special in order to get it to work the same way across all modern browsers.

If you are going to use JavaScript to jump to a different spot in the page you are better off using location.hash = ‘id’; rather than the location.replace as then you are updating the specific field within the location directly rather than relying on the browser working it out.

Simply specifying:

<a href=“doc2.htm#xx”>

in document 1 will always have that link to:

<div id=“xx”>

in document 2 without any need for any special code at all.

The most modern browser that doesn’t work in is Netscape 4.

I definitely gave this a shot before giving up on it. Performing it at the end of the document did append the hash to the URL but it did not do anything because (I assume) the page was still loading.

The Onload command is new to me. It generated a bit hope when I looked it up. Good news: This solution does work on a few major browsers (Ex: Opera). Bad news: Does not work on most major browsers. The command is effectively ignored on Refresh. “Ignored” is not really the correct description. “Does not work as expected” is probably more correct. On many web browsers, the page is moved to the top on Refresh.

Writing a solution for just one major browser (Ex: Internet Explorer) is very do-able but writing code that will work on all major web browsers is very difficult especially with vendors pumping out a new release every few months. I’m hoping for an HTML solution that will consistently work on all major browsers.

This is effectively covered in my response to deathshadow60. See above. Running a raw location.hash statement does not appear to work (regardless of where it’s loaded in the document) if it is fired while the document is loading.

Refresh versus Reload. I am definitely out of my league on this topic but aren’t the terms interchangeable in modern browsers? Some browsers do offer additional keyboard shortcuts to reload/refresh ignoring cached content (Ex: Ctrl+F5) but other than reloading images (and such) from external servers, it does the exact same thing. To avoid confusion, more and more modern browsers have stopped using the word “Refresh” (Ex: Chrome, Opera, Safari, et. al.) to indicate a page reload.

With that all being said… Exactly how a browser responds to a Refresh/Reload request may be root of my problem. To save time, space, etc., each vendor decides what commands will be executed (and how) on a Refresh/Reload request. Without consistency, it may difficult to find an HTML solution that works on all major browsers.

Thank you deathshadow60 and felgall for your feedback. If I’ve misunderstood anything (probably) or if you have any additional ideas/information, please make a noise.