29

How can I do this?

In firefox the link opens in a new tab... I don't want users to have to set settings of their browsers for this...

I want a pop-up to appear with contact-form whenever user clicks 'contact' on my main page.

How should I do this?

Getz
  • 3,983
  • 6
  • 35
  • 52
  • 11
    You should really reconsider. The user should be in control of his environment. http://www.joelonsoftware.com/uibook/fog0000000249.html – soulmerge Dec 02 '09 at 17:40
  • 1
    I can only speak for myself, but: Sites that open Popups for anything automatically get deducted points in my book. If you really want a new dialog, maybe a JavaScript Dialog - modal or not - is more user-friendly? For example, the Dialog from jQuery UI: http://jqueryui.com/demos/dialog/#default – Michael Stum Dec 02 '09 at 17:53
  • @soulmerge how much do you actually know about the OP's use case? is this a public facing website for B2C? perhaps it's an admin tool for her/him and colleagues to use internally (unlikely - but in my case its an admin tool). Joel I believe is the reason Excel didn't get rewritten and is still the most annoying application I use (other than word), so I'm not sure he's the best person to follow in terms of UX. just a thought. – MemeDeveloper Feb 18 '21 at 22:13
  • I just went to the link you posted to Joel's site... hamburger menu and folder icon top left with no hover title.... not someone to follow for UX - unless you want to make a system as bad as insta's webapplication for browsers. – MemeDeveloper Feb 18 '21 at 22:17
  • My real point is "never say never"... unless you're sure you know all the details of all the use cases that might ever be coded against ever. ! ;) – MemeDeveloper Feb 18 '21 at 22:19
  • @soulmerge the user should enjoy their environment and find it productive is a better statement. I don't want infinite control, I want to get stuff done and to enjoy life. – MemeDeveloper Feb 18 '21 at 22:22

9 Answers9

34

You cannot control this - it's entirely at the discretion of the user-agent; which is the point, after all. All you can specify is that the page be opened in a different viewpane context, and it's up to the user to decide how they want your window to take up their screen space/taskbar list/Alt-Tab shortcuts etc.

In fact I'd go even further and say that if at all possible you should avoid opening up a new tab/window at all. I know that I get a little annoyed when websites do this, and it feels a bit clunky and 1990s what with all the Ajax and floating divs and magic we have nowadays.

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
33

This is working fine

window.open(yoururl, "Popup", "location,status,scrollbars,resizable,width=800, height=800");

For all parameters see window.open

Edited Date:- 12th Aug, 2021 If you does not want to use JavaScript Function, then You can write Inline Script for Open Page in New Windows, & it is support all the modern browsers

<a href="https://stackoverflow.com/" onclick="window.open('https://stackoverflow.com/', 'newwindow', 'width=400, height=250'); return false;">with href</a>


<a href="#" onclick="window.open('https://stackoverflow.com/', 'newwindow', 'width=400, height=250'); return false;">without href</a>

You can see Live Working example on this jsfiddle.net link

Harsh Patel
  • 1,032
  • 6
  • 21
M2012
  • 3,465
  • 2
  • 20
  • 27
  • 1
    This should have more upvotes.. While it is ultimately up to the browser, using this indicates to the browser that it is supposed to be a popup. – John Harding Jul 15 '15 at 19:59
  • How does this fare against a popup blocker? – Kevin Dice Aug 06 '15 at 19:58
  • 1
    Agree with John: the answer from Kiran os OK. The basic window.open don't work: Firefox (eg) open in a new tab. But Kiran's method is right. – Peter Nov 27 '15 at 18:20
11
<a href="javascript:window.open('http://example.com/popup.html','blank')">Contact</a>
bdukes
  • 152,002
  • 23
  • 148
  • 175
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
3

I believe this is a browser-only setting that cannot be set from HTML or Javascript.

Joe
  • 46,419
  • 33
  • 155
  • 245
2

When I was looking into this issue I noticed that the "height" and "width" portions of the window.open() function were what made the link open in a new window instead of a new tab.

So to open a similarly-sized browser I just passed the window.innerHeight and window.innerWidth to the window.open() function.

I would have just commented on @M2012's answer, but I don't have enough points yet...

Hope this helps!

vancy-pants
  • 1,070
  • 12
  • 13
0

If you want to use this thing using Code Behind then it will also work...

protected void lnkAddNewWorkout_Click(object sender, EventArgs e)
    {
  //  Response.Write("<script>window.open('MyCreatedWorkout.aspx','_blank');</script>");    //it Open in new tab

    ResponseHelper.Redirect("MyCreatedWorkout.aspx", "_blank", "menubar=0,width=700,height=700");

    }



 public static class ResponseHelper
    {
        public static void Redirect(string url, string target, string windowFeatures)
        {
            HttpContext context = HttpContext.Current;
        if ((String.IsNullOrEmpty(target) ||
            target.Equals("_self", StringComparison.OrdinalIgnoreCase)) &&
            String.IsNullOrEmpty(windowFeatures))
        {

            context.Response.Redirect(url);
        }
        else
        {
            Page page = (Page)context.Handler;
            if (page == null)
            {
                throw new InvalidOperationException(
                    "Cannot redirect to new window outside Page context.");
            }
            url = page.ResolveClientUrl(url);

            string script;
            if (!String.IsNullOrEmpty(windowFeatures))
            {
                script = @"window.open(""{0}"", ""{1}"", ""{2}"");";
            }
            else
            {
                script = @"window.open(""{0}"", ""{1}"");";
            }

            script = String.Format(script, url, target, windowFeatures);
            ScriptManager.RegisterStartupScript(page,
                typeof(Page),
                "Redirect",
                script,
                true);
        }
    }
}

Refrenced By: http://www.codeproject.com/Tips/317410/Response-Redirect-into-a-new-window

Durgesh Pandey
  • 2,314
  • 4
  • 29
  • 43
0

You cannot control this.

But you can try to open in a new window and then show an alternative solution if the browser rejects the attempt.

You can accomplish this by checking the return value of window.open().

var win = window.open(strUrl, strWindowName);
if (!win) {
    // Call failed. Handle it here. 
    // You can for example open content in a model or show a button that 
    // will open content in a new tab
}

window.open documentation: https://developer.mozilla.org/en-US/docs/Web/API/Window/open

HoffZ
  • 7,709
  • 6
  • 36
  • 40
0

Best way to to open a new window in HTML.

<a href="#" onclick="MM_openBrWindow('http://www.google.com','','resizable=yes,width=800,height=600')"> Google</a>

<script>
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
</script>
Blackspade
  • 45
  • 1
  • 5
  • The question is specifically in regard to forcing a new window to open rather than a tab, and as the [MDN article](https://developer.mozilla.org/en-US/docs/Web/API/Window/open#Usability_issues) for window.open() indicates, it is unlikely the behavior will be as desired in a modern browser, though this would seemingly be the most straightforward JS to open a window. – Jmills Dec 28 '16 at 20:03
0

This is also easy way but link is not visible it is appropriate for button action

<a onclick="window.open('print.html', 'newwindow', 'width=300,height=250');"> Print</a>
A.A Noman
  • 5,244
  • 9
  • 24
  • 46