14

I am firing the Window.open(); command. which opens the link page in another tab. what i want is when i click the link, the link will open in new window but should be on the same page.

Is that possible ?

presently i am using like this.

function AddToDatabase(url) {
            window.open(url, "_blank");
}
Moiz
  • 2,409
  • 5
  • 27
  • 50
  • Browser settings control how window.open is handled. – j08691 Mar 08 '13 at 17:15
  • 1
    Is there any plugin that we can handle on client side ? – Moiz Mar 08 '13 at 17:16
  • 4
    Your question does not make sense. Do you want to open the current page in a new window? That's possible, though it's not up to your code whether that is actually a new window or tab. – Matt Ball Mar 08 '13 at 17:18
  • 1
    @MattBall, That's not entirely true. You can confine it to the browser window using the target of `_self`, however you can't do the old-school "pop-under" windows that he's looking to do using window.open natively. Check my answer for a shadier version of doing this. – Josh Burgess Mar 08 '13 at 17:23
  • It means i want to open window in new tab. but i want that i should be on the same page. – Moiz Mar 08 '13 at 17:33
  • @Moiz: So you want to open a new tab in the background? This sounds likes something that you *can't* do. First off whether it's a tab or window is up to the browser/user settings. Second, pop-unders, which is what you want to do, are blocked by most browsers because users hate them. – gen_Eric Mar 08 '13 at 17:40
  • yes true :( i guess that is not possible – Moiz Mar 08 '13 at 17:43
  • @Moiz: Why do you want this? What are you trying to do in the long run? – gen_Eric Mar 08 '13 at 17:43
  • It's a convenience to the user. In my case, the user is in the middle of javascript in one screen, but the system gives the user a chance in another screen to do a needed activity. It is confusing to the user to automatically transfer to the other screen. – VectorVortec Nov 10 '17 at 00:51
  • https://stackoverflow.com/a/59509486/1916821 is one of possible tricks to solve this issue – Vaha Dec 28 '19 at 09:03

6 Answers6

17

Use _self instead of _blank.

window.open(url, "_self");
  1. _blank - URL is loaded into a new window. This is default
  2. _parent - URL is loaded into the parent frame
  3. _self - URL replaces the current page
  4. _top - URL replaces any framesets that may be loaded name - The name of the window

For further details. See This Link

Umair Saleem
  • 1,055
  • 5
  • 19
  • Isn't this the same as `window.location.href = url`? And, by that I mean *not* a pop-up. – gen_Eric Mar 08 '13 at 17:25
  • window.location.href is a property that will tell you the current URL location of the browser. If you reset the property page will be redirected. Where as window.open will open new window for that url, additionally you can pass parameters to windows.open for instance show tool bar, show status bar, width and height of the window etc. – Umair Saleem Mar 08 '13 at 17:31
  • @UmairSaleem: In Chrome, `window.open(url, "_self");` did *not* open a new window! It just redirected me. – gen_Eric Mar 08 '13 at 17:31
  • 1
    @RocketHazmat: _self - URL replaces the current page....for new window use _blank – Umair Saleem Mar 08 '13 at 17:42
  • @MattBall: sorry, how i can revert it ? – Umair Saleem Mar 08 '13 at 17:42
5

I know that this question is uber old, but it perfectly described the problem I needed answered, and I was able to come up with a decent solution, so I thought I'd post it here. It's an interesting workaround.

Essentially you set a timeout to delay forwarding the current page to the new url, and then open the current url in a new tab. So:

function open_hidden_tab(new_url) {
    var thisTimeout= setTimeout(function() {
        window.location.href= new_url;
    }, 500);
    var newWindow= window.open(window.location.href);
    if(!newWindow) {
        clearTimeout(thisTimeout);
        alert('Please allow pop-ups on this site!');
    }
}
open_hidden_tab('https://google.com');

Obviously, you should show some kind of error message on your site instead of using the annoying alert function, but it works for the purposes of this example.

Hope this helps someone!

Programmer Dan
  • 289
  • 2
  • 10
3

The code you have should not be changing the page. How are you calling AddToDatabase()? Is it from an a href tag? If so the default action is taking place from the link and you need to prevent that.

You can set the href attribute to javascript:void(0)

<a href="javascript:void(0)" onclick="AddToDatabase('myUrl')">Add URL</a>

Or you can have the onclick attribute return false

<a href="#" onclick="AddToDatabase('myUrl'); return false;">Add URL</a>
Rusty Jeans
  • 1,426
  • 9
  • 10
2

you can use this.

 <a href="terms.html" onclick="window.open(this.href, 'targetWindow', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=400,height=600');return false;" />Terms & Conditions</a>

its works for me

Md Safiul Alam
  • 153
  • 1
  • 6
0

Here's an example of a pop-under:

var url = "example.com";
window.open(url, "s", "resizable=yes, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, copyhistory=no").blur();
window.focus();

If that's what you're looking for, go nuts. It's extremely unethical from a web development sense, but it can be done.

Josh Burgess
  • 9,327
  • 33
  • 46
  • In Chrome, this seems to work if the window is already open. The 1st time will pop-up, but if that window is already open, then pop-under works. – gen_Eric Mar 08 '13 at 17:29
  • 1
    Yeah, the pop-under is something that web browsers have taken care to try to prevent. – Josh Burgess Mar 08 '13 at 17:30
0
<html>
<body>

<script>
myWindow=window.open('','','width=200,height=100')
myWindow.document.write("<p>This is 'myWindow'</p>")
myWindow.focus()
</script>

</body>
</html>

You should determine the width and the height of the window.

Look at this demo: http://jsfiddle.net/XyJW5/ but don't forget to allow popup in your browser for this website

SaidbakR
  • 13,303
  • 20
  • 101
  • 195
  • 1
    If I have to allow popups for a certain site, you're doing something wrong. If you put this code in a click handler, then it won't be blocked (by most browsers). – gen_Eric Mar 08 '13 at 17:37
  • In Chrome, this resulted in a pop-up, not a pop-under. – gen_Eric Mar 08 '13 at 17:38
  • @RocketHazmat This example is just for showing the importance of defining width and height for the window only. – SaidbakR Mar 08 '13 at 17:51