-2

I have a page, https://einsteinmedneuroscience.org/wp-content/scroll/master.html# with two undisplayed iframes. There is a little arrow button for each one of them to trigger them to display. The iframes are long, so neither of them will fit on the page, and the page itself gets a vertical scroll bar. (This is the intended effect.)

First, click the top button to display the first iframe.

Second, scroll to the bottom of the page and now click the second button.

Two things happen, the second iframe appears and the whole page is spontaneously scrolled back to the top of the page. What on Earth has caused that and is there a way to prevent it (to keep the second iframe in view)?

Here's the code on this page

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    
     <script type="text/javascript">

      function observeIFrameChange(mutations){
        for (let mutation of mutations) {
             if (mutation.oldValue = "display:none") {
                 mutation.target.style.height = `${mutation.target.contentWindow.document.body.scrollHeight}px`;

             }

        }
      }

      $(document).ready(function () {

          function buttonClickFunction() {
          /* swap the arrow icon when clicked */
            var theCurrentCharacter = this.innerHTML;
            var theClickedAnchorTag = document.getElementById(this.id);
            var theIFrameTag = document.getElementById(theClickedAnchorTag.dataset.tag);
            if (theCurrentCharacter === "·?") {
                this.innerHTML = "??";
                
            /* when the iframe is displayed, by default it displays 150 pixels height. */
            /* to assign its actual height, we need to set up a mutation observer for attribute changes */
            /* because for whatever reason, it won't work here */
                var observer = new MutationObserver(observeIFrameChange);
                observer.observe(theIFrameTag,  { attributes: true, childList: false, attributeOldValue: true });

            /* display the iframe */
                theIFrameTag.style.display="inline";



            } else {
                this.innerHTML = "·?";

                theIFrameTag.style.display="none";
            }
        }

        /* assign buttonClickFunction to little arrow buttons */
        var theBtnRow1 = document.getElementById("btn_row1");
        var theBtnRow2 = document.getElementById("btn_row2");
        theBtnRow1.addEventListener("click", buttonClickFunction);
        theBtnRow2.addEventListener("click", buttonClickFunction);

Note that the HTML for the button element was like so

<a id='btn_row1' href="#" class="buttonshape" data-tag="firstiframe">➡︎</a>

This was where the error was.

mauricev
  • 73
  • 1
  • 6
  • 2
    The obvious reason for closing is, that you haven't included any code. We can't debug code we can't see. A link to your page is not sufficient. When you'll fix the issue, this question becomes useless for future readers. We expect you to make your part of the debugging. Isolate the problem to the smallest code still reproducing the issue, and include the code to your question. see [mcve]. – Teemu Jun 25 '20 at 04:25
  • Please put everything needed to ask in your post itself. A MRE includes cut & paste & runnable example showing your result. A MRE is minimal so find the first point in your code execution that doesn't return what you expect & say what you expect & why with reference to documentation. – philipxy Jun 26 '20 at 04:43
  • **This question is being [discussed on Meta](https://meta.stackoverflow.com/questions/398778/i-have-edited-my-question-about-javascript-please-evaluate-for-reopening).** Please take discussions about whether or not it should be re-opened there. Thank you. – Cody Gray - on strike Jun 26 '20 at 07:15

1 Answers1

1

Your anchor tags have href='#' in them, which is usual for anchor tags to point back to the same page.

However, hashrefs like this are also used to move the browser page around, looking for the id="".

For each of your buttons, change the href to be '#' + the div id: enter image description here

EDIT: With reference to this answer "What is href=“#” and why is it used?" https://stackoverflow.com/a/21397285/4301860

About hyperlinks:


The main use of anchor tags - <a></a> - is as hyperlinks. That basically means that they take you somewhere. Hyperlinks require the href property, because it specifies a location.

Hash:


A hash - # within a hyperlink specifies an html element id to which the window should be scrolled.

href="#some-id" would scroll to an element on the current page such as .

href="//site.com/#some-id" would go to site.com and scroll to the id on that page.

Scroll to Top:


href="#" doesn't specify an id name, but does have a corresponding location - the top of the page. Clicking an anchor with href="#" will move the scroll position to the top.

This is the expected behavior according to the w3 documentation.

BM-
  • 616
  • 5
  • 15