I have a View in Razor that when you have one set ViewBag, I want to open a url to print a report. I'm using Jquery code below, however, is opening in a new window and I want to always force to open a new tab, regardless of user configuration.
if (#ViewBag.Imprimir != null)
{
window.open('/Audiometria/Imprimir/' + #ViewBag.Imprimir, '_blank', 'toolbar=0,location=0,menubar=0');
}
It is important to note that whether or not a window.open() opens up a new tab or window depends on the browser implementation and user settings, and you can't force opening in a new tab.
Based on your window.open() parameters, most browsers will decide to open it in a new window, which makes sense since you have provided parameters to turn off certain UI elements (toolbar, location, menu bar etc.) that are usually present by default for all tabs, and usually it is not possible to turn off those elements only for a single tab.
If you omit the third parameter from your example it should open properly in a new tab in most new browsers, since you opening a plain new page without any specific UI settings for the browser, so there is a bigger chance that the browser will open it in a new tab.
window.open('/Audiometria/Imprimir/' + #ViewBag.Imprimir, '_blank');
Related
I've got a page that presents a bunch of part numbers to my users. I want a new window to open when the user clicks on a part number. The MSDN page for NavigateURL seems to indicate that I just put Target="_blank" in the properties for the Hyperlink, but that just pops a new tab (in Chrome if it matters).
How do I get it to pop a new window instead of a new tab? I tried to delete the DataNavigateUrlFormatString property because it seemed extraneous, but it didn't like that and told me that "Target was not a valid option for a hyperlink", or something to that effect (if it's important I can get the exact error).
Here's my code:
<asp:HyperLinkField HeaderText="Part Number"
DataTextField="partNumber"
DataNavigateUrlFields="partNumber"
DataNavigateUrlFormatString="PartLookup.aspx?partNumber={0}"
NavigateUrl="PartLookup.aspx?partNumber={0}"
Target="_blank"/>
The browser being used decides how to handle being given a target="_blank", and you can not control whether it chooses to make a new tab or window.
You can use Javascript to force the opening of a new window, but I do not believe that can be done through your HyperLinkField directly.
See this issue for Javascript
I wrote a simple code to submit a sign up form with Selenium. Before submit, driver should come from home page to sign up page.
var firefox = new FirefoxDriver();
firefox.Navigate().GoToUrl("http://mywebsite/home");
If I print firefox.Title, it shows me title of home page currectly
And in home page, there is a sign-up button. Sign up button link is bellow.
<a target="_blank" href="SignUp.jsp">Register Here</a>
To navigate to sign up page, I wrote a line:
firefox.FindElement(By.CssSelector("a[href='SignUp.jsp']")).Click();
After this, driver shows me the sign up page in new window of firefox browser. To navigate driver to the sign up I wrote firefox.Navigate();
Now If I print firefox.Title, it shows me title of home page again.
Please help me to find out problem. Thanks in advance.
You pretty much grabbing the same title since the you never switched to newly opened window
// Get the current window handle so you can switch back later.
string currentHandle = driver.CurrentWindowHandle;
// Find the element that triggers the popup when clicked on.
IWebElement element = driver.FindElement(By.XPath("//*[#id='webtraffic_popup_start_button']"));
// The Click method of the PopupWindowFinder class will click
// the desired element, wait for the popup to appear, and return
// the window handle to the popped-up browser window. Note that
// you still need to switch to the window to manipulate the page
// displayed by the popup window.
PopupWindowFinder finder = new PopupWindowFinder(driver);
string popupWindowHandle = finder.Click(element);
driver.SwitchTo().Window(popupWindowHandle);
// Do whatever you need to on the popup browser, then...
driver.Close();
driver.SwitchToWindow(currentHandle);
And, after switching to new window you should get new title.
However, this window handles process is utterly confusing to me. Selenium .Net bindings provide PopupWindowFinder class to handle windows.
Gratitude to JimEvans for his nice works and this
Use
firefox.SwitchTo().Window(handle);
where handle is one of instances found in firefox.WindowHandles. This will switch between the different window instances. You can find more information in the docs for IWebDriver.SwitchTo().
Does anybody know how to open an URL in a new tab from C# code?
I have tried with
Response.Write("<script type='text/javascript'>window.location.href('../Documents/doc.pdf','_blank'); </script>");
Response.Write("<script type='text/javascript'>window.open('../Documents/doc.pdf','_blank'); </script>");
Response.Write("$('#pageContent_Send').click();");
with
$("#pageContent_Send").click(function () {
window.open("../Documents/doc.pdf");
return false;
});
and it did not work, using "window.open" I get an "Pop-up Blocker" browser warning.
There are a few options for opening a new tab that won't get blocked.
You can have it open the URL with an anchor click, like so click me
You can submit a form to a blank target, giving a similar result, but from a form. This is useful if you need to post, but can also be useful for get. Like so <form method="get" action="<your destination>" target="_blank"><button type="submit">Click Me</button></form>
You can use JS with window.open but it has to be tied to an active click event. I see in your original post that this got blocked, but if you're triggering it on a click event directly (i.e. not using a setTimeout or an async call or something else that delays the window opening) then this should work in all browsers. Since you're trying to "fake" it by forcing a click, the browser is going to block this every time unless you explicitly allow it.
<a href='www.xyz.com' target='_blank'>Click me to open me in new tab</a>
Form -> Submit
Takes you to view of the new entry.
How can I open a new tab that sends you to a new actionresult return File(document, mimeType); while still directing you on the main tab?
I'm at a loss, I cant open a new tab via ASP.NET Code, nor invoke two different actionresults.
Your problem should be solved on the client side, not only on the server. Maybe I misunderstood something, but if you want to open a new browser tab, you just need to place a <a href="#Html.Action("SomeAction")" target="_blank"> (Razor syntax) in your main tab.
You would need to do it client side. You have 2 options:
1) A shell page that has nothing except for the following javascript:
$(document).ready(function() {
window.open("#Model.DownloadUrl", "");
window.location.href = "#Model.RedirectUrl";
});
2) Modifying the view to check for a DownloadUrl via javascript and download it if available:
$(document).ready(function() {
if (#!string.IsNullOrEmpty(Model.DownloadUrl)) {
window.open("#Model.DownloadUrl", "");
}
});
In either case, there's no way to force the new window to be opened as a new tab. That's a user's browser setting. The javascript above opens a new tab in the version of Chrome and Firefox I have but not in IE.
I have a site which uses Shadowbox-JS to bring up a settings page when a user clicks on a little icon. The settings are there for the user to be able to customise their view of what they're looking at in the main application.
The settings page is a full (but compact) .aspx page which has all the relevant code for the settings to be applied, updated to a database, read from a database etc, so that's all nicely dynamic. What I want is for a postback to occur once the user closes the shadowbox so that the view on the main page automatically updates to reflect their changes.
The following link appears once the user presses 'save' in the settings area:
<a onclick="window.parent.location.href = window.parent.location.href;">Settings saved. Please close this window</a>
This basically just refreshes the whole page and of course, in the process the shadowbox is no more. This approach works fine but the problem is the user also has the option to close the shadowbox by clicking outside of it. I need to capture when this happens and cause a postback (or page refresh) when this happens so that no matter what the settings are always applied when the shadowbox is closed.
I've found the answer, I needed to add an option to the Shadowbox.init() function as follows:
Shadowbox.init({
onClose: function () {
window.location.href = window.location.href;
}
});