最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Target='_blank' to show in new window, NOT new tab, possible? - Stack Overflow

programmeradmin10浏览0评论

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?

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?

Share Improve this question edited Apr 29, 2015 at 10:12 Getz 4,0536 gold badges36 silver badges52 bronze badges asked Dec 2, 2009 at 17:36 user188962user188962 6
  • 11 You should really reconsider. The user should be in control of his environment. joelonsoftware.com/uibook/fog0000000249.html – soulmerge Commented Dec 2, 2009 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: jqueryui.com/demos/dialog/#default – Michael Stum Commented Dec 2, 2009 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 Commented Feb 18, 2021 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 Commented Feb 18, 2021 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 Commented Feb 18, 2021 at 22:19
 |  Show 1 more comment

9 Answers 9

Reset to default 37

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

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.

<a href="javascript:window.open('http://example.com/popup.html','blank')">Contact</a>

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

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!

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

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

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>

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>
发布评论

评论列表(0)

  1. 暂无评论