1

Let’s say there is this html file that just has a button.

In the JavaScript file (added into the html file), jQuery is used to give the button a click function and it executes:

window.open('some-url');

This code opens the url in a new page but I want to be able to do things with that page whether it be to close it or to scroll it to a position.

I was think about doing this:

var web = window.open('some-url');
web.scrollTo(0,400);

However, this piece of code does not scroll to y-position of 400 in the newly opened link. Is there a way where I can make the ‘window’ refer to not ‘this’ but the some-url window?

3 Answers3

0

The short answer is no.

The long answer is no because it would be a huge security vulnerability if you could execute scripts on another website. Most websites link important sections of content with jump links https://blog.hubspot.com/marketing/jump-link-same-page.

Basically the website has an anchor tag with and ID attribute, and that ID attribute can be referenced with in the URL like https://www.some-ur.com/test#jump-to-id

It does seems that window.open does support a third argument where you can specify a top value as well https://www.w3schools.com/jsref/met_win_open.asp

Tommy May
  • 970
  • 10
  • 13
0

With Simple Anchor Tag

id="abc"
<a href="#abc"></a>

With Javascript

Current Window

This one is easy. a window will always refer to the current window. New Popup Window

If you open a window using window. open, you’ll get a reference to the window you opened.

var newWin = window.open('https://www.google.com', 'windowName');

Existing Popup Window

If someone has opened a popup window with window.open, and you know the window name they used, you can get a handle on that window by calling window. open without an empty string as the URL, and with the same name.

var existingWin = window.open('', 'windowName');

This will even work if the popup window was opened from a different frame on the same domain.

In modern browsers, this will even work if the window was opened from a different frame on a different domain. But be warned — this can cause some strange behavior in IE.

Popup Windows as they open

If you’re writing a library and you need to keep a collection of popup windows that are opened by any other library or javascript code, you can monkey patch window.open:

var windows = [];

var winOpen = window.open;

window.open = function() {
    var win = winOpen.apply(this, arguments);
    windows.push(win);
    return win;
};
Tech Zone
  • 403
  • 4
  • 9
0

First and foremost, why do you want to do that?

Secondly, is there content on the page you are referencing at the 400 y position? I know this question sounds stupid, but I have to ask b/c It worked for me.

Copy/Paste This:

<!DOCTYPE html>
<html>
<body>

<p>Click the button to open a new window called "MsgWindow" with some text.</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
  var myWindow = window.open("", "MsgWindow", "width=200,height=600");
  myWindow.document.write("<p>This is 'MsgWindow'. I am 200px wide and 900px tall!</p><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />p");
  myWindow.scrollTo(0,400);
}
</script>

</body>
</html>

To this: https://www.w3schools.com/jsref/met_win_open.asp

halfer
  • 18,701
  • 13
  • 79
  • 158
  • I put (0,400) in order to simply my question. Your example is great, however it doesn’t refer to a url link (which I need). But nonetheless cheers mate. – Darren Wang Mar 26 '19 at 04:30
  • I see what you mean here. I need to get back to my home planet, but before I go, I have a link to what looks like an interesting answer: https://stackoverflow.com/a/7914458/11258141... Please let me know how this turns out if it gets solved. – Super-WhyDoYouWantToDoThat-Man Mar 26 '19 at 04:44
  • 1
    If you want to make the theme of all of your questions "why do you want to do that", then that's fine. However, please do not add meta-commentary relating to that into your posts - commentary about the posts does not belong in the posts. At a stretch, you can put it in the comments. Please also do not ask people to upvote or not to downvote - with some minor exceptions people can (and will) vote how they please. – halfer Mar 27 '19 at 23:09
  • 1
    Finally the "hot date mom" material is just not suitable for a professional site, and that _will_ get you downvotes. Please refrain from that sort of stuff here; even if it is just a metaphor, it's an inappropriate one. – halfer Mar 27 '19 at 23:10
  • If you thought that hot mom metaphor was inappropriate, you should check out my haiku work on my profile. – Super-WhyDoYouWantToDoThat-Man Mar 27 '19 at 23:35