How to scroll an element into view inside a window opened with window.open()

Viewed 21

this is my code:

var x = 582,y = 500,w = 200,h = 200;
var term = Math.random().toString(16).substr(2, 8);
var win = window.open(`https://www.example.com/search?q=${term}`, "", "width=" + w + ",height=" + h+",status=no");
win.moveTo(x, y);
win.document.scroll(0,findPos(win.document.getElementById("id_rc")));

Works as normal, but when the window is opened, the element is not scrolled into view.

1 Answers

Add your desired ID into URL itself (#id_rc), so that browser would automatically scroll that element to view, just like with normal references in URL:

var x = 582,y = 500,w = 200,h = 200;
var term = Math.random().toString(16).substr(2, 8);
var win = window.open(`https://www.example.com/search?q=${term}#id_rc`, "", "width=" + w + ",height=" + h+",status=no");
win.moveTo(x, y);
Related