1

I'm trying to open a window and scroll down "a little" in the newly opened window.

function createPop(url, name)
{
  var newwindow;  
  newwindow=window.open(url,name,'width=760,height=950,toolbar=0,menubar=0,location=0,popup=1,status=0');
  if (window.focus) {newwindow.focus()}

  newwindow.scrollBy(0,400);
}

A new window opens, but scrolling doesn't working. I tried many different ways (scrollTo ...), but nothing changed. What is wrong?

JanezKranjski
  • 225
  • 3
  • 15
  • `window.focus` is a **function**, not a Boolean. Also, see https://developer.mozilla.org/en-US/docs/Web/API/Document/hasFocus – Roko C. Buljan Nov 30 '21 at 06:43
  • I'm not sure. As a noob, I took an example from Github (if I remember). I thought that this line will set focus on the newly opened window. I now comment it and it still works, but "scrollBy" still doesn't. – JanezKranjski Nov 30 '21 at 06:45
  • Is the scrollbar visible? It must be. If the page is too short there will be no effect. – wazz Nov 30 '21 at 07:23
  • Yes, the scrollbar is visible. – JanezKranjski Nov 30 '21 at 07:25
  • 1
    @JanezKranjski if `newwindow` is not in your domain, you cannot scroll it. *"Blocked a frame with origin --- from accessing a cross-origin frame"*. If it *is*, than you could postMessage to the Opener Window to trigger a scroll. – Roko C. Buljan Nov 30 '21 at 07:53
  • Well, the URL in the new window is from one of our company's domains, but I understand (I hope) what you meant. It is not on the same domain from where the call ("window.open") was started. – JanezKranjski Nov 30 '21 at 08:03
  • Is there any way to simulate scrolling down using the keyboard? Or is this also disabled because of security reason? – JanezKranjski Dec 08 '21 at 08:57

1 Answers1

0

To scroll a newly opened page, use onload function. but you must use a specific condition in if statement.

In my case, I used the windows.location.href as a condition.

If the location.href.includes(string added to location.href by clicking the link on the other page ) then onload scroll to (you can use link or coordinates).

here are the steps to make it clear:

1- HTML

add / after # so the link will go to the top of the newly opened page. and we will use that as a condition

This link is on a page called work, this link will open index.html page.

    <a href="index.html#/work" class="nav-btn-work">WORK</a>

When the new page opens, there will be a #/work in its location.href

2- Now let's use that in javascript to scroll the newly opened page:

    let pageUrl = window.location.href;
    
    if (pageUrl.includes("#/work")) {
      setTimeout(() => {
        location.href = "#work"; 
      }, 500);
    }

// #work is a section in the middle of the new page and after opening the new page, it will scroll to it. 

I waited for half a second before scrolling. you can change or remove that.

I hope this helps. it works for me with no errors or problems.

Mad7Dragon
  • 1,237
  • 1
  • 10
  • 21