Go to anchor via xmlhttp

J

joker197cinque

I have a div (overflow auto) that load an html file.

It is loaded via xmlhttp after a click event on the parent document.

Simple:

var fileName = "/ex.html";
xmlhttp.open("GET", fileName,true);
xmlhttp.onreadystatechange=function() {
mydiv.innerHTML = xmlhttp.responseText;
}
xmlhttp.send(null);

But what about an anchor in ex.html ? I put a named anchor in bottom
part of the ex.html file and I would like the div will scroll exactly
to that point, after the click event.

If I specify "/ex.html#anchor" xmlhttp object raise an error because
it doesn't find the file (correct!).

How can I achieve this ?

Thanks
 
J

joker197cinque

You can use anchorElement.scrollIntoView(), if scrollIntoView is
available, or you can try window.location.hash = "anchorName".

Window.location.hash sometimes works!

If I add an alert before window.location.hash = "anchorName", after I
click "OK" I can correctly see text related to anchor.

But if code is executed "as is" with no interval, it does not work.

-----
WORKS
-----

var fileName = "/ex.html";
xmlhttp.open("GET", fileName,true);
xmlhttp.onreadystatechange=function() {
mydiv.innerHTML = xmlhttp.responseText;
}

xmlhttp.send(null);
alert("wait");
window.location.hash = "anchorName"


-----
DOES NOT WORK
-----

var fileName = "/ex.html";
xmlhttp.open("GET", fileName,true);
xmlhttp.onreadystatechange=function() {
mydiv.innerHTML = xmlhttp.responseText;
}

xmlhttp.send(null);

window.location.hash = "anchorName"

I obviously can't add an alert ... and I tried also with setTimeOut
function with no luck.

Can you help me please ?

Regards.
 
J

joker197cinque

You can use anchorElement.scrollIntoView(), if scrollIntoView is
available, or you can try window.location.hash = "anchorName".


Thanks Conrad, I did it using something like this:

function timedHash(an)
{
setTimeout(function() {browseAnchors(an);}, 50);
}

Best regards.
 
D

David Mark

You can use anchorElement.scrollIntoView(), if scrollIntoView is
available, or you can try window.location.hash = "anchorName".

Those techniques are not equivalent (the latter navigates.)

[snip]
 
D

David Mark

Thanks Conrad, I did it using something like this:

function timedHash(an)
{
        setTimeout(function() {browseAnchors(an);}, 50);

}

You only created an illusion. The request is asynchronous. You don't
know how long it will take to complete. Put the navigation in the
callback (after setting the innerHTML of the div.)

[snip]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top