Join the AMA (Ask Me Anything) with the Firefox leadership team to celebrate Firefox 20th anniversary and discuss Firefox’s future on Mozilla Connect. Mark your calendar on Thursday, November 14, 18:00 - 20:00 UTC!

Az oldal korlátolt funkcionalitással fog rendelkezni, amíg elvégezzük a felhasználói élményt javító karbantartást. Ha egy leírás nem oldja meg a problémáját, és kérdést tenne fel, akkor a támogatási közösségünk a @FirefoxSupport Twitter oldalon tud segíteni, vagy az /r/firefox oldalon a Redditen.

Támogatás keresése

Kerülje el a támogatási csalásokat. Sosem kérjük arra, hogy hívjon fel egy telefonszámot vagy osszon meg személyes információkat. Jelentse a gyanús tevékenységeket a „Visszaélés bejelentése” lehetőséggel.

További tudnivalók

A témacsoportot lezárták és archiválták. Tegyen fel új kérdést, ha segítségre van szüksége.

Id in url is not locating element in DOM on page load.

  • 2 válasz
  • 1 embernek van ilyen problémája
  • 5 megtekintés
  • Utolsó üzenet ettől: hugoharrijeffreys

more options

I am working on a server-side rendered app which uses a templating language to render HTML. Before submitting a form the user has the option go back and change one of their inputs. This is achieved by adding the ID ref that I want to scroll to the end of the href URL.

e.g. <a href="/form/edit#form-error>Change</a> would go to the /form/edit page and automatically scroll to the element with an ID of form-error.

This works perfectly well in Chrome and Edge. However, in Firefox the page simply renders in the centre. A page refresh does the same thing. However, if i click the url bar and press enter it scrolls to said element. I think Firefox maybe loads DOM elements, javascript etc in a different order is the issue? Any help with this much appreciated!

I am working on a server-side rendered app which uses a templating language to render HTML. Before submitting a form the user has the option go back and change one of their inputs. This is achieved by adding the ID ref that I want to scroll to the end of the href URL. e.g. <a href="/form/edit#form-error>Change</a> would go to the /form/edit page and automatically scroll to the element with an ID of form-error. This works perfectly well in Chrome and Edge. However, in Firefox the page simply renders in the centre. A page refresh does the same thing. However, if i click the url bar and press enter it scrolls to said element. I think Firefox maybe loads DOM elements, javascript etc in a different order is the issue? Any help with this much appreciated!

Összes válasz (2)

more options

Navigation to anchors is based on vertical distance from the top of the page. If the position of the element changes after Firefox initially computes it, Firefox usually ignores that later change. You can use a script to fix the position after load, for example:

    var elId = window.location.hash;
    if (elId.length > 1){
        el = document.getElementById(elId.substr(1));
        el.scrollIntoView({block: "center"});
    }

I guess the question is how/when to fire it. Does your application already have some code that runs on load or on pageshow?

more options

Thanks jscher2000 for such a quick reply. Sadly I don’t think this will work for me. My app is rendered via a templating engine/node so window, document etc isn’t available to me. Also I can’t attach a script to the rendering file as it is a form page so would be vulnerable to XSS attacks.