How to close the webpage on its own on clicking a button - javascript

I have a page where a <button> is kept on which onClick() event fires and redirects the new page in new tab. Meanwhile I want the parent page to close itself as soon as the <button> is clicked with opening the new page in new tab. Its basically for some security feature for the website. How can this be done?
Here is my <button>, let me know where can i correct myself.
<button formtarget="_blank" type="submit" name="submit" onClick="javascript:window.open('quiz.php?unit_id=<?php echo $fnc->encode($unit_id) ; ?>');self.close();" value="submit" class="btn btn-info" style='margin-left: 35%;margin-bottom: 10px;' ><i class="glyphicon"></i> Start Quiz</button>

you can close window by below function
function close(){
setTimeout("window.close()", 500);
}
But keep in mind that : Scripts may close only the windows that were opened by it.

The window close and open new tab
<script language="javascript">
function quitWindow(cmd)
{
if(window.open("your_new_tab_page.htm")){
if (cmd=='quit')
{
open(location, '_self').close();
}
}
return false;
}
</script>
<input type="submit" onclick="return quitWindow('quit');" value="Close This Window and open new Tab" />
If you use firefox, you should about:config by writing url bar and set
dom.allow_scripts_to_close_windows = true
otherwise does not work firefox

I think this might work.
<script>
function openWindow( url )
{
window.open(url, '_blank');
window.focus();
window.close();
}
</script>
<button onclick="javascript:openWindow(yourHref);return false;">Submit</button>

You cant close your webpage if the user clicked the link of your website from any other place other than your website, due to security reasons.

Related

window.open('url', '_self') not working

So i wanted to open a new page replacing the current one, i found that the method should be putting the second parameter on _self but nothing happen...
By the way, if i use the _blank parameter or i left it empty it opens in a new page. The rest of the function works good, but i can't find a way to close the current page and open the new one that i want.
Here is the javascript and the html buttom that call the function.
<button id="rgstr_btn" type="submit" class="btn btn-info" onClick="store()">Register</button>
<script>
function store() {
localStorage.setItem('nome', nome.value);
localStorage.setItem('pw', pw.value);
window.open('url', '_self');
}
</script>
Button has a type attribute which defaults to submit: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-type
While this does not affect "everyday" buttons, if the button resides in a form, this way it will submit the form, and result in some page loading, which clashes with your own attempt.
You can just add a type="button" attribute to the button to avoid that:
<button id="rgstr_btn" type="button" class="btn btn-info" onClick="store()">Register</button>
^^^^^^^^^^^^^
windows.open() opens the URL in a new window.
To replace the URL in the current window, use:
window.location.href = 'http://example.com';

Navigate new page in same tab/window using button

I've been trying to navigate a page to another page in the same window (web browser = Google Chrome, Mozilla Firefox and Internet Explorer) using button. The steps are,
The page will popup message for confirmation.
If the user click ok, then the page will navigate to other page in the same tab/window.
If the user click cancel or x, it stays on the current page.
I have tried using
window.open("home.php","_self"); and window.location.href="home.php"; and document.location.replace("home.php"); and location.replace("home.php"); and location.assign("home.php");
but it doesn't work. How do I fix this?
<span title="Click to register the new user"><button type="submit" id="submit" onclick="myFunction()">Register User</button></span>
<script>
function myFunction() {
confirm("Proceed?");
window.open("home.php");
}
</script>
Problem seems to me is, possibly you are using form with button which leads to reload.
<button type="submit" id="submit" onclick="myFunction()">Register User</button>
Make the type="submit" to type="button". Also major browser do have popup blocker enabled where this line may fail window.open("home.php");
Small change in script as follows:
function myFunction() {
if(confirm("Proceed?")) //<--- ok/yes = true, no/cancel/close = false
location.href = "home.php";
}
Small demo:
var ask = function() {
if (confirm('Proceed ?'))
location.href = "http://espn.go.com/";
};
<button onclick="ask();">Watch ...</button>
window.location.href
is not a function . You have to use it as
window.location.href="home.php";
Updated your code and this works
<span title="Click to register the new user"><button type="submit" id="submit" onclick="myFunction()">Register User</button></span>
<script>
function myFunction() {
if( confirm("Proceed?") )
window.location.href="home.php";
}
</script>

Self exit button

I got my page to open with JS, with
<a href="javascript:openNewWindow();">
Now, the webpage will open in _self. I need to find a way to get a link, that will close my window. This doesnt work:
<a href="javascript:window.close();">
Now what to do? Anyone knows a better way to close self window? The second script has to be in the opened page.
This is a correction of my previous answer:
newwindow=window.open();
newdocument=newwindow.document;
newdocument.write("Hello World.<input type='button' value='close' onclick='window.close()' />");
newdocument.close();
as you can see, window.close() works ok.
For a specific example, you can use this -> create 2 files:
1. windowOpener.html
<html>
<body>
<input type="button" value="open" onclick="window.open('newWindow.html')" />
</body>
</html>
2. newWindow.html
<html>
<body>
<input type="button" value="close" onclick="window.close()" />
</body>
</html>
if you'll run windowOpener.html and click 'open' it will open 'newWindow.html'. clicking 'close' in the new opened window, will close it. voila.

window.close() not working on pop up with pop up

I have a Customer Info form that has anchor tag "close" that should close the current window. This customer form is being opened as pop up. The Customer form also has a search btn that when clicked it pop ups the search form that is being run by a javascript called searchOrder.js. So basically it is a pop up inside a pop up. The customer form's close btn is not working but when the reference to searchOrder.js is removed, it begins to work. Also, when the search pop up is open and I click the "close" anchor tag, it closes the search pop up but never the current window which is the customer form.
Ive tried a lot of solutions already but nothing is working. I used self.close(), window.opener.close(), made it a btn instead of a link etc.,c alled a function onclick
function closeWindow() {
var closeRef;
window.opener='x';
closeRef = window.open('','_parent','');
//or closeRef = window.open("",name);
closeRef.close();
}
heres my gsp code:
<g:form method="post" name="CustomerInfoForm" target="_parent" role="form">
<div class="id="closeLink">Close</div>
<button id="searchOrderButton" type="button" class="button" onclick="searchOrder(document.forms[0].summaryMessage.value,'summaryMessageText','${createLink(action:'searchOrder')}','${createLinkTo(dir:'images',file:'closeButton.gif')}')" value="Search Order">Check Order</button>
</g:form>
How do I make the current window (customer info ) close? tnx
You should use a function like this to close your popup window :
<g:form method="post" name="CustomerInfoForm" target="_parent" role="form">
<div class="id="closeLink">
Close
</div>
<button id="searchOrderButton" type="button" class="button" onclick="searchOrder(document.forms[0].summaryMessage.value,'summaryMessageText','${createLink(action:'searchOrder')}','${createLinkTo(dir:'images',file:'closeButton.gif')}')" value="Search Order">Check Order</button>
</g:form>
<script type="text/javascript">
function openpopup()
{
my_window = window.open("","myPopup","status=1,width=100,height=100");
}
function closepopup()
{
if(false == my_window.closed)
{
my_window.close ();
}
}
</script>
EDIT : Don't forget to open your popup with the openpopup() function :
Open Popup Window
You can shorten window.close() to just close() and shorten window.open() to open(). Maybe you'll find what you need here, though:
http://www.w3schools.com/js/tryit.asp?filename=try_win_closed
http://www.w3schools.com/jsref/obj_window.asp

Close popup window opened by a different domain

I have a link in abc domain which opens a popup window and opens a page in a different domain. I need to add a button on the popup which calls window.close() and closes it via javascript. Is is even doable? can I close a popup using windo.close which has been opened by a different domain?
Do you mean something like this:
function openWin(){
myWindow=window.open("http://www.google.co.uk","","width=200,height=100");
}
function closeWin(){
if(myWindow){
myWindow.close();
}
}
hooked up to these buttons:
<input type="button" value="Open 'myWindow'" onclick="openWin()" />
<input type="button" value="Close 'myWindow'" onclick="closeWin()" />
it's a tad crude but should work... I'd also make it unobtrusive if I were you, I've used obtrusive code just to get an answer to you quickly

Categories

Resources