Force html link to open in a new window - c#

There's no easy way to explain this but, I have a website, that links to a online game I've made using asp C#. The website has a link to the game so when the user clicks the link on the site, my game pops up! Simples...
But I want it to pop up in a new Window! but not a browser window... Just... A Window :s
for Example, if you go on www.dofutoshiki.com and click on the "Play Futoshiki in our online player" link, it opens a new window with, just the game. No browser functionality.
I was wondering if anybody out there could shed some light on the subject...
Ive Tried:
Click me!
but it just opens a new tab in the browser!
Thanks!
Alex

If I get this right, you actually want a browser window but without the chrome/UI.
For this, you need to use window.open() (javascript) rather than a target attribute.

http://www.pageresource.com/jscript/jwinopen.htm will help you.
You have to open the new window by using javascript and disabling the unwanted functionality by passing arguments.

You will probably need some javascript for this. What you need to look in to is the
'window.open(...)' function.
Here are a few examples.
And here you can play around with it.

Use javascript Window.open method
Click me!
<script>
function openGame(){
window.open(blahblah.aspx,'Play Game','width=900,height=790,scrollbars=yes,dependent=yes,toolbar=no,location=no,status=no,directories=n o,menubar=no,status=no,resizable=yes');
}
</script>

You can use Jquery popup window plugin it is easy to use and more flexible also.
http://swip.codylindley.com/popupWindowDemo.html

On site you are referring to, browser window is opened. It's configured so that it does not have toolbar, status bar, menu bar, etc.
You can take a look at window opening code in HTML source of the page you are referring to:
<a onClick="window.open(this,'playsample','width=900,height=790,scrollbars=yes,dependent=yes,toolbar=no,location=no,status=no,directories=no,menubar=no,status=no,resizable=yes');return false;">

Is javascript an option?
With javascript it would be something like this:
<a href="#" onclick="window.open('blahblah.aspx', 'game','status=0,toolbar=0,width:400,height:300');" > Click here for the game! </a>

Use this
Click me!
Or even better, put the onclick code in a function of your own so you can reuse it.

Related

Selenium not Clicking Link

I want to reset modem with selenium. But I am facing a problem. I am trying to click but no click operation has been done. modem click is zyxel.
<li style="cursor: pointer;" class="subItem" id="maintenance-reboot"><span class="arrow"> </span><a>Reboot</a></li>
I tried
driver.FindElement(By.LinkText("Reboot")).Click();
I also tried with id "maintenance-reboot" but nothing work for me.
Then I try to go directly link.
http://192.168.1.1/pages/maintenance/reboot/reboot.html
page came but no frame only reboot page and I clicked button but nothing change.
I think the mainframe required for reboot.
try this 3 things:
driver.FindElement(By.Xpath(".//li[#id='maintenance-reboot']/a")).Click();
or
driver.FindElement(By.Xpath(".//a[contains(text(),'Reboot')]/..")).Click();
or
driver.FindElement(By.Xpath(".//a[contains(text(),'Reboot')]/../..")).Click();
one of them must work for you
Alter solution: go to the page and execute Javascript "btnReset();"
((JavascriptExecutor) driver).executeScript("btnReset();");

Open page in a new tab from C#

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>

How to make the 'Are you sure you want to navigate away from this page' warning message in browser?

I have a web page that contains a textbox and a submit button. When the user edits the text in the textbox and clicks another link (not the submit button) how do I display the 'Are you sure you want to navigate away from this page' popup message?
I have researched this on the net and found a few javascript examples. Is this the only way you can do this? If not, what is the best way to do it?
This is one of the multiple ways to achieve the same thing
function goodbye(e) {
if(!e) e = window.event;
//e.cancelBubble is supported by IE - this will kill the bubbling process.
e.cancelBubble = true;
e.returnValue = 'You sure you want to leave?'; //This is displayed on the dialog
//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
}
window.onbeforeunload=goodbye;
got it from here open js
Only the unload() event will work on JS. You can't manage it on the server.
Check out the answer to this other question on SO, it is very similar to your question
How to show the "Are you sure you want to navigate away from this page?" when changes committed?
Simple solution
window.onbeforeunload = confirmExit;
function confirmExit() {
return "Are you sure you want to leave this page?";
}
4guysFromRolla.com - Prompting a user to Save when Leaving a Page
You cannot use the onbeforeunload window method as it gets triggered by multiple ways like back and forth browser navigation links, refreshing the page, closing of the page, clicking on the links.
What i feel you have to bind the link tag for which you want display the navigation away message and then use the function for the status message display
window.addEvent('domready',function(){
$$('a').addEvent('click', function(e) {
//leaving(); function u wrote for displaying message
});
});
function leaving(e) {
if(!e)
e = window.event;
// return code for the displaying message
}
If you want to do this in a way that guarantees it will work on almost all browsers, use the JQuery library. The following describes the unload event.
http://www.w3schools.com/jquery/event_unload.asp
It's exactly for purposes like yours.
Just to elaborate a little, you would have to download the jquery js library and reference it in your project/page, but you'll probably want to do that eventually anyway.
If you want to control this from the server side, you can dynamically emit the jquery call in the OnPreRender.
Look into Jquery's .beforeunload property. Here is an example:
$(window).bind('beforeunload', function(){ return 'Click OK to exit'; });
Please note, beforeunload canot prevent a page from unloading or redirect it to another page for obvious reasons; it would be too easy to abuse. Also if you just want to run a function before unloading, try the following:
$(window).unload(function(){ alert('Bye.'); });
Finally, don't forget to referrence jQuery in your head tag by using:
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
The above gets you the latest version from the internet and saves you the trouble to download it, and of course you can do so optionally, but I am just trying to get your thing to work asap.
Oh, I also found an example for you. Click here to see a page that calls a function before it closes. Hope this helps bud.
I was able to get this to work with Andrei G's answer. I would add on that to get it to work in Chrome, add this to the end of his goodbye function:
return "";

How to call a popup window in C#

I have webform1,webform2. I have a submit button in webform1.When the submit is clicked, it has to perform some function then based on the result i have to show different data (let say accept/decline images) in the webform2(POPUP). When this popup is displayed user should not be allowed to make any changes to webform1.
Let me know how to achieve this.
Thanks in advance
Just add the following code in the pageLoad method:
YourButtonID.Attributes.Add("onclick", "window.open('WebForm2.aspx',null,'left=400, top=100, height=350, width= 580, status=no, resizable= no, scrollbars= yes, toolbar= no,location= no, menubar= no');");
EDIT: Place the code in the Button_Clicked event handler instead in pageLoad...works in both ways, but it is better solution...
Aren't you asking for a "modal" popup?
You should try with ShowModalDialog method.
Alternatively you can try AJAX Modal Popup... Click Here for the sample...
To achieve this in my web application I used raw HTML+CSS+Javascript.
Basically what you need is a top level div added to body with 100% size. Then another div with margin:auto, sized the way you want.. You could make that earlier div transparent or semi transparent using CSS.
This will only work on the good browsers ;)

Closing an IE 'popup' window with script

Is it at all possible to disable the "The webpage you are viewing is trying to close the window. Do you want to close it?"?
I understand that this is the product of years of virus, and malicious script activity, but in legit app code, (ASP.NET), is there any way to like "register" your app as an APP, or flags you can pass to an IE Popup so that it will not display this when it closes?
The code I'm using is done from within the C# code behind:
ClientScript.RegisterClientScriptBlock(GetType(), "save", Utils.MakeScriptBlock("window.close();"));
The Utils.MakeScriptBlock is just a function that does what you might expect. It 'injects' a <script...> tag with the code in it...
It's probably not possible to get around this, or else, all the script kiddies would just use that trick, but I thought I'd ask, as I can't be the ONLY one using simple IE "popups" as (pseudo)modal dialog boxes.
This code happens in my ButtonSave_Click() routine, after everything has passed validation, etc...
** EDIT **
Just for reference, here is the code that OPENS the popup, when the ADD button is clicked:
This is in Page_Init()...
ButtonAdd.Attributes.Add("onclick", "window.open('Add.aspx', 'ADD_WINDOW', 'scrollbars=no,width=550,height=550,location=no,menubar=no,resizable=yes,directories=no,status=no,toolbar=no'); return false;");
You can close the window without the popup if the window was opened by your script. Does that help?
Edit:
You're already opening the window with script. Change your client script to call self.close().
ClientScript.RegisterClientScriptBlock(GetType(), "save", Utils.MakeScriptBlock("self.close();"));
I believe not. As you state to help prevent malware.
In the end the browser does not know that you are not evil. See RFC 3514 for a similar idea.
I highly doubt it's possible on your end - sounds like something a user would have to specifically disable in their IE settings.
If the user opened the browser window, you can't close it; but if the window was opened via script, you can. Sounds like you just need an initial page that the user starts at with a "click here to begin" link, from which you open a new window for the main portion of the site; when they're all done, close the popup and they're left with their initial browser window with the "click here to begin" message.
Bizarre, but I've found that
<script type="text/javascript">
function closeWindow()
{
window.close();
return false;
}
</script>
and then calling
return closeWindow();
usually gets around this.

Categories

Resources