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?

8
  • window.focus is a function, not a Boolean. Also, see developer.mozilla.org/en-US/docs/Web/API/Document/hasFocus Commented Nov 30, 2021 at 6: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. Commented Nov 30, 2021 at 6:45
  • Is the scrollbar visible? It must be. If the page is too short there will be no effect. Commented Nov 30, 2021 at 7:23
  • Yes, the scrollbar is visible. Commented Nov 30, 2021 at 7: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. Commented Nov 30, 2021 at 7:53

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.