Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Button click open in new Window

Hi I need to open my view in a new window on this button click but it opens in the same window. This is the syntax I'm using.

<button  id="btnPrintReport" onclick="location.href='@Url.Action("LoadReports", "DashBoard", new { target="_blank" })'" >Print</button>

Have I missed anything? Thanks.

like image 465
Kanishka Dananjaya Avatar asked Oct 13 '14 07:10

Kanishka Dananjaya


2 Answers

Try window.open to open link in a new tab

onclick="window.open('@Url.Action("LoadReports", "DashBoard", new { target="_blank" })')"

DEMO

like image 58
Arijit Mukherjee Avatar answered Sep 27 '22 21:09

Arijit Mukherjee


As far as I know, <button> won't work with target. Same goes for input type="button" ... /> elements:

You need to use an anchor and style it with css as a button

<a [email protected]("LoadReports", "DashBoard") id="aPrintReport" target="_blank" class="button">Print</a>

<!--taken from http://stackoverflow.com/questions/2187008/styling-an-anchor-tag-to-look-like-a-submit-button-->
.button {
    text-decoration: none; font: menu;
    display: inline-block; padding: 2px 8px;
    background: ButtonFace; color: ButtonText;
    border-style: solid; border-width: 2px;
    border-color: ButtonHighlight ButtonShadow ButtonShadow ButtonHighlight;
}
.button:active {
    border-color: ButtonShadow ButtonHighlight ButtonHighlight ButtonShadow;
}
like image 30
Marco Avatar answered Sep 27 '22 19:09

Marco