What is the best way to inject child nodes into parent nodes without causing the view to shift to the location where the element is being

While I am rendering elements and appending them to a parent div, my screen keeps jumping to the bottom-most element as it loads instead of maintaining the current view. Ideally, it should start at the top of the screen and remain there without any sudden jumps. As I scroll down and more elements are rendered, the view should stay constant while the remaining content loads.

Below is an excerpt from my JavaScript file:

function loadMultipleElements(amountToLoad, url) {
    var parentDiv = document.getElementById("instance");
    for(var i = 0; i < amountToLoad; i++) {
        var iframe = document.createElement('div')
        iframe.innerHTML = '<iframe src=\"' + url + '\"></iframe>';
        parentDiv.appendChild(iframe);
    }
}

This is how I'm loading it in the HTML file using infinite scroll functionality from jQuery:

<script>
    loadMultipleElements(5, "https://www.example.com");
        $(window).scroll(function() {
            if($(window).scrollTop() == ($(document).height()) - $(window).height()) {
                loadMultipleElements(5, "https://www.example.com");
            }
        });
</script>

When I run this on my localhost, all elements render correctly upon opening but the screen initially jumps to the bottom for each new render, causing disruption. This issue is exacerbated by infinite scrolling as it triggers the jQuery function continuously due to the constant jumping to the bottom.

EDIT: I have also included a Plunker link, though I am currently unsure how to implement infinite scrolling with jQuery on Plunker. For now, there is a fixed load value of 10, which still results in the screen scrolling towards the bottom with each render.

http://plnkr.co/edit/fUrJek3RAicHG98lUrC9?p=preview

Answer №1

The issue you're experiencing is due to Bing automatically focusing the search bar, causing your browser to scroll down to the iframe for text input.

You can check out a slow-motion illustration of this here.

To work around this issue, one solution is to utilize the iframe sandbox attribute in HTML5 to prevent Bing from focusing the search bar. After the iframe finishes loading, JavaScript functionality can be reenabled. An example can be found here.


However, there are some drawbacks to this approach:

  • Disabling JavaScript during iframe loading also disables desired JavaScript functionality (e.g., 'recent stories').

  • It may seem like a hacky workaround.

  • The purpose of this workaround may not be immediately apparent.


PS: For more information on the iframe sandbox attribute, visit W3Schools. You can also learn more about sandboxed iframes with this tutorial.

PPS: If you have a better solution for this issue, I'm open to suggestions and would love to hear them.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Order of operations during synchronous $.ajax request handling

I'm grappling with understanding how synchronous ajax calls impact the order of execution as I encounter some peculiar bugs. // (1) $.ajax({ async: false, url: url0, dataType: 'json', success: function(data) { // (2) }); / ...

"The Ajax BeginForm for a list is only returning the first element of the model, or nothing at

Attempting to trigger an action using an ajax-beginform. This is the model structure being used: public class MyViewModel { public List<SubViewModel> SubViewModels {get;set;} } public class SubViewModel { public string Name {get;set;} ...

Tips for showcasing content on a jQuery calendar

I have a jQuery calendar implemented on my website and I would like to show a list of local holidays on the calendar. The calendar is functioning correctly with the script below, but I just need help displaying the holidays. <script> $(document).r ...

Is there a way to retrieve information from all pages of a table on a webpage using Chrome?

On a webpage, I have a table containing 100 rows that are displayed 20 at a time. By clicking "next page," I can view the following set of 20 rows and the table view is updated accordingly. I am able to access the data for each batch of 20 rows using dev ...

What is the best way to scale down my entire webpage to 65%?

Everything looks great on my 1920x1080 monitor, but when I switch to a 1024x768 monitor, the content on my webpage becomes too large. I've been manually resizing with CTRL+Scroll to reduce it to 65%, which works fine. Is there a code solution using CS ...

HTML various button designs - such as a cogwheel

I need a button on my Angular/Electron project that resembles a gear icon. I came across these resources: here and here. However, when I tried to implement them, they didn't work as expected. Currently, the button looks like this: <button class= ...

How can an SVG circular progress bar be created using stroke dasharray?

Can anyone help me with adding dashes between the line in this progress bar I'm trying to recreate? Any suggestions would be greatly appreciated. https://i.sstatic.net/KdwtzYGy.png This is my progress so far: https://i.sstatic.net/3KTjic5l.png I h ...

Utilizing jQuery to dynamically load CSS files on a webpage

For my website, I have implemented 4 different .css files to handle various screen resolutions. Here is the innovative jquery code I've developed to dynamically load these files based on the width of the screen. <link id="size-stylesheet" rel="st ...

What is the best method to adjust the width of the <option> tag within the <select> tag using CSS?

<!DOCTYPE html> <html> <head> <title>Page Title</title> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="s ...

What is the best way to target the specific DIV using a jQuery selector?

My dynamic div is filled with other divs... <div id="wrapper"> </div> //javascript for(//lots of times){ var d = document.cloneNode('divModel'); d.foo = {//a lot of stuff }; document.getChildById('wrapper').append ...

Incorporating middleware in Next.js to remove route protection

Looking to remove the protection for the login page, and here is how my folder structure looks: https://i.stack.imgur.com/klPYV.png This is the middleware I am using: import { NextResponse, NextRequest } from "next/server"; export async functi ...

Tips for inserting an HTML table into a div element using JQuery/JavaScript

I currently have a navigation bar with only two items. While I may potentially add more items in the future, for now I am content with just these two. Upon opening the page, my goal is to display the content of the first item in the navigation bar. Each it ...

The decision will be dependent on the outcomes provided by the $resource promise

I have been working on calling my API with AngularJS to retrieve a list of 'reports' and then displaying them in a modal or saving the sale depending on whether any results were returned. I've been struggling with this for a while and would ...

I'm looking to achieve the same effect repeatedly with JQuery every time I click

Below is my JQuery code. I have implemented functionality where clicking on the '+' link will slide down a text box, which is working fine. However, I want the same effect to occur every time the '+' link is clicked again. Can anyone he ...

Positioning of the footer using CSS: A query

What is the best way to position a footer if we know its height? For instance, if the height of the footer is 100px: footer { position:absolute; bottom: 100px; background: red; } If this approach is not recommended, could someone assist m ...

Error alert: The function name used in the import statement is not defined

I've taken the initiative to organize my JavaScript functions into separate files based on their respective web pages (index, login, register, etc.), and then importing them all into a main JS file. This helps with code maintenance and readability, pr ...

submit content to an iframe on a separate webpage

Work has been a challenge for me lately as I try to achieve a specific task. The starting page features a textbox and a button. My goal is to post the value entered in the textbox to an iframe on a different web page called iframeholder. The catch is tha ...

Is there a method to extract the y value for a specific x value that is not part of the current data set in a Chart.js graph

I have a set of data points like { x: 5, y: 10 }, { x: 10, y: 15 }, { x: 20, y: 25 }. With Chart.js, I can easily create a chart based on these points. Is there a method to retrieve the y value for a specific x value, such as 13, from the rendered chart? ...

Three.js Ellipse Curve Rotation

Purpose My goal is to create an EllipseCurve () where a camera will move. The Approach I Took to Achieve the Goal Below is the code snippet for creating the ellipse: var curve = new THREE.EllipseCurve( 0, 0, 1, 1, 0, 2 * Math.PI, false, ...

Bootstrap 5: Position the cards in close vertical alignment with one another

For my latest project, I have been working on creating a responsive card collection using Bootstrap 5 and Vue 3. By utilizing the container and row classes, I was able to ensure that all cards are aligned at the same level. However, in order to keep the ti ...