open new window without the browser giving warning that is a popup - c#

I am working on mvc2 and i'm trying to open from javascript in a new window something.
if i only use inside the js function window.open("action/action/"+param) like this
function(){
window.open("action/action/"+param);
}
it works perfectly and the browser does not warn me that it is a new popup that i'm opening.
But if i make an ajax call before the window.open, the browser tells me that is a new popup and if i accept it.
if (json.data && json.data.URL)
{
window.open("action/action/"+param);//opens in a new window only if i accept popups
}
how can i make it work without the browser considering it as popup?

You can't popup a window "randomly" it needs to be right after the user has clicked something or did an action so that the browser doesn't think it's a random ad popup.

The window has to open as a direct result of the user's input. For example, you could open a new window inside a click handler. It's not full proof by any means, but it's one measure the browser can take to try and prevent unwanted popups.

Related

I've opened a SSO popup, how do I click a link in the popup?

I'm accessing an SSO through an ASP/C# page and using the following code to open up a popup window:
string s = "window.open('" + getSSOlink(ah1.InstitutionUserID, cardnumber) + "','popup_window', 'width=980,height=600,resizable=yes');";
Page.ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);
After opening the popup, I need to click 2 links deep into the the popup window navigate to a specific part of the page. The id on the first page is id25, and on the second page is cuRewardsLink, and it is the same every time the SSO opens. After the 2nd link is clicked, the user will be directed to a Rewards system.
I'm trying to find a way to open a new browser window that navigates automatically to the Rewards page. Ideally, they would not see the loading of the first two pages in the popup, just the final destination. The links within the popup do not have static URLs, there is an AJAX function returning information from the click(s) that create the link.
After the popup opens, I'm not sure how to access the HTML elements in the current browser window (the popup window). I tried using the WebBrowser class to navigate to the page, wait for loading,and click the links, but got a "ThreadState Exception - ActiveX Control cannot be instantiated because the current thread is not in a single thread apartment":
WebBrowser browser = new WebBrowser();
browser.Navigate(getSSOlink(ah1.InstitutionUserID, cardnumber));
Thread.Sleep(1000);
browser.Document.GetElementById("id25").InvokeMember("Click");
Thread.Sleep(1000);
browser.Document.GetElementById("cuRewardsLink").InvokeMember("Click");
browser.Visible = true;
Any help or thoughts would be appreciated,

How to close current Browser window and redirect to a previously opened window for the same browser

i have two asp page,first one is home and second one is test. In home page user can select the type of test they want to take up, and after pressing start button a new window is open for taking the test. What i want to achieve is , after completing the test i want to close the test window and redirect to another page, and this redirect should hit the previously opened home window.
You don't clarify that you have popup window or blank window i am aspecting for the popup
In submit click button press
String x = "<script type='text/javascript'>window.opener.location.href='**Your new url of new page after completing test**';self.close();</script>";
ScriptManager.RegisterClientScriptBlock(this.Page,this.Page.GetType(), "script", x,false);
From self close you will be able to close current window and by window.opener.location.href you will able to redirect to new url
I hope this will help you
regards....:)
Yes you can do these 2 ways:
Window.Open() /.showmodalDialog() and keep parent and child open as well, or
On the home page click on your start button. Use code Response.Redirect("~/Test.aspx"); assuming it resides with Home.aspx. Take your test using ASp.Net wizard control or hiddens divs what ever suits you easy. Manipulate data on test page , save and get result to and from database using SqlConnection and SqlCommand assuming you have Sql Server as backend. Hold the table or any value in either cache or Session and throw it on your home page. Do whatever you want.
This will be not possible. What you can do is to redirect the user to the home page after completing the test, but this will be in the same windows.

Redirect webpage to same tab in asp.net

I'm using a asp.net hyperlink control to open a web URL when a user click the hyperlink.
What I want to do is, say user click the hyperlink, so it should open new tab (not a new window).
if the user clicks the link again, it should not open a different tab. It should redirect user to the same tab which opened last time.
Update: here i found
Popup = window.open(URL, "LoginWindow");
This will redirect user to same window when the link is clicked. I'm using this functionality in popup windows. This works perfectly fine with Chrome & Firefox but not in IE.
It always open a new window rather than redirecting to the already opened one. Any Idea to solve this?
Regards
You can also try target="MyWindow" so it always open in this new window/tab. This way we can avoid opening new window/tab for every anchor link we click on.
The attribute target="_blank" is your only option. Much will depend on users' browser specific settings, you cannot change. In modern browsers this will open a new tab, in others a new window.
After opening in a new tab (new tab vs new window is a browser option, not an HTML option, see Open link in new tab or window) with
target="_blank"
you need to set the hyperlink target to self either when generating your the page in c# or in JavaScript
target="_self"
See http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hyperlink.target.aspx
int i=1;
if(i==1)
{
Hyperlink1.target="_blank";
}
else
{
Hyperlink1.target="_self";
}
Try this code.Hope this will work for you
if (ViewState["hasvalue"].ToString() == "Clicked")
{
HtmlPage.Window.Navigate(new Uri("form2.aspx"), "_self");
}
else // First Time it will be opened in New TAB
{
Hyperlink1.target="_blank";
Hyperlink1.NavigateUrl="form2.aspx";
}
// Assign this value to session
ViewState["hasvalue"] = "Clicked";
}

Asp.net c# Button click call a procedure and Also opens in a target_blank

I am trying to make a button that when clicked call a procedure but also opens in a pop-up. I can't find how to do it because all the search i did only tells me to put it on te clientclick :
<asp:Button ID="cmbGen" runat="server" Width="240px" Text="Générer le rapport" OnClick="cmbGen_Click"></asp:Button>
the onclick opens up a pdf, and its not working well on Ie, so, to solve this i would like the pdf to opens in a pop up
not sure what i could do.. anyone got an idea ?
EDIT
The code is pretty big, but basicaly, depending on what checkboxed were checked, it will create a pdf file and show it. this works pretty well, but it opens up in the current page, i would like to make it in a pop-up
A Button always submits a postback to the server on the current window, so you can't directly tie a new window to it. You'll need to write some javascript to do that, and open a popup window.
You can do this lots of ways - you can hook up an event handler to the button so when it's clicked, it immediately opens a new window, and that window is pointed to your server code which returns the PDF. Or you can do a regular postback, and return some javascript that pops up a new window. But either way, javascript is the only way to get a popup from a form button.
Liam's suggestion of making a link instead of a button is probably the simplest method - you can throw an image on that link to make it look like a button if you want.
EDIT
Based on your comment on the other answer, your simplest bet would be to return some javascript on the button click method, using ClientScript.RegisterStartupScript or whatever Microsoft is recommending these days. You can do whatever logic you need to first, then get that into a new handler either through session or querystring parameters, and have the client pop up a new window pointing to that handler.
Can't see you c# so it's not 100% sure what you want but why use a asp:Button at all:
Générer le rapport

Open new window from code-behind

I need to open a new window from code-behind on post-back if a specific radio button is selected.
Is there any way to do this?
Thank you.
You can use RegisterStartupScript to send a window.open script to run once the page has loaded.
However, this will cause the majority of popup blockers to get in your way.
I think this should work ;-)
Add some javascript to your radio button to open a new blank window before you post back. This makes it so popup blockers won't block your popup, since it's opened in response to a users click. See this link for how to do this part.
Then, allow the postback to happen as normal and on page load, register a startup script to tell your already existing window to go to a new url.
String script = "window.open('popupPage.aspx', 'myPopup')";
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "someId", script, true);
Note that in javascript, when you call
window.open(url, 'myPopup')
if a window already exists with that name it'll return it instead of creating a new window... So your popup won't get blocked!
You will need to run javascript on postback
Simple sample of RegisterStartupScript:
RegisterStartupScript("id1", "<script type=\"text/javascript\">alert(\"I'm from JavaScript.\");</script>");
New windows can be very sketchy, depending on the content that you need to present you might consider using an in window pop-in if you will. You will avoid pop-up blockers that way. If you can give more details we can give better answers.

Categories