This question already has answers here:
window.close and self.close do not close the window in Chrome
(17 answers)
Closed 8 years ago.
I have a web page with a countdown and redirect to the previous page which works great, but instead of redirecting to the previous page, I want the page (current browser tab) to close. disappear. self-destruct.
I have tried substituting my redirect history.go(-1) with window.close() but it didn't work.
JS:
var countdownfrom=9
var currentsecond=document.redirect.redirect2.value=countdownfrom+1
function countredirect(){
if (currentsecond!=1){
currentsecond-=1
document.redirect.redirect2.value=currentsecond
}
else{
history.go(-1)
return
}
setTimeout("countredirect()",1000)
}
countredirect()
HTML:
<form name="redirect">
<h1>Oops! I think you meant to use a URL...</h1>
<h2>this page will self destruct in</h2>
<form>
<input type="text" size="1" name="redirect2">
</form>
<h2>seconds</h2>
<h3>Go back to where you came from and try again!</h3>
the html doesn't look right, can you even have a form nested in another form? It works fine though.
Corrected HTML:
<h1>Oops! I think you meant to use a URL...</h1>
<h2>this page will self destruct in</h2>
<form name="redirect">
<input type="text" size="1" name="redirect2">
</form>
<h2>seconds</h2>
<h3>Go back to where you came from and try again!</h3>
This should be an informative answer: why doesn't my window.close work
Long story short, you probably are not meeting the criteria to close a window with JS.
Related
I am fairly new to the world of coding and am currently designing something using WAMP to assist the misses with her maths. In short I have a page with some basic maths questions on and I have JavaScript running on it to check whether the answers are correct or not.
What I would like to know is, can I use JavaScript or something similar to add in a link that if clicked will open a popup or something similar that the user can write a few bits down to help working out the sums?
I have seen a <button onclick="window.open('whitespace.html');">Thinking Space</button> but this doesnt allow the user to write anything down, obviously as its just a link to another page.
A very basic sample, just pointing you in a possible direction:
<button onclick="window.getElementById('myDiv').style.display = 'block'">Thinking Space</button>
<div id="myDiv" style="display: none">
<textarea name="content" cols="40" rows="5">Test message</textarea>
<button onclick="window.getElementById('myDiv').style.display = 'none'">Close</button>
</div>
i think you can use window.promt
https://developer.mozilla.org/de/docs/Web/API/Window/prompt
Maybe not the best answer but works for me. I created a link to a blank page that the user can then open. They can work the questions out, minimise it, forget about it and if they were to try to open a fresh one, close it or try to refresh it, they will get a message asking if they are sure they want to continue. Works exactly as Id hoped.
<textarea placeholder="Please be aware no data will be saved here.
If closed or refreshed all data will be lost"</textarea>
<script>
window.addEventListener("beforeunload", function(event) {
event.returnValue = "Reloading will cause any notes to be lost.";
});
</script>
This question already has answers here:
Prevent form redirect OR refresh on submit?
(7 answers)
Closed 2 years ago.
I have been trying to figure this out for past few hours, I have form and when users will submit it,
new window popup will occur, however I want it delayed slightly, setting setTimeout did not work and I have no idea why.
example:
<form method="post" target="print_popup" action="https://example.com" onsubmit="window.open('about:blank','print_popup','width=1000,height=800');">
<input type="hidden" name="param" value="foobar">
<input id="1" type="submit" value="Submit request">
Think you will need to set the target attribute of the form to _blank to allow for opening a new context:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form
I would also check chrome/your choice of browser isn't blocking your popup
This question already has answers here:
Is it safe to use anchor to submit form?
(3 answers)
Closed 2 years ago.
I'm making a web page with 20 different questions on the index page. The user clicks "Submit" after every question.
<form action="./index.php" method="post" id="element">
The information is then posted and sent to the database. Of course this makes the index page reload and display at the very top of the page. This makes for a lot of scrolling.
Is there a way to make it so the page reloads and scrolls back down to the place where it left off?
The answer was found here: https://stackoverflow.com/a/7983093/4101210
To use an anchor to submit a form would require the use of JavaScript to hook up the events. It's not safe in that if a user has JavaScript disabled, you won't be able to submit the form. For example:
<form id="form1" action="" method="post">
Submit!
</form>
If you'd like you can use a <button>:
<button type="submit">Submit!</button>
Or stick with what we all know:
<input type="submit" value="Submit!" />
You can style all three of them, but the latter two don't require JavaScript. You probably just need to change some CSS somewhere if you're having border issues.
The code that answered my question was by joshua bissot. He showed how to put the token in the action and name the anchor to make this work.
https://stackoverflow.com/a/25918771/12140988
<!-- add the anchor token at the end of your action statement -->
<form method='post' action='this_page.php?put_peram=token#anchor_name'>
<input type='submit' value='click here'>
<!-- put the anchor right above where you want the page to index -->
<a name="anchor_name></a>
I am trying to submit a simple form that is in a pop-up dialog and then close the dialog.
The best article I've seen is Submit a form in a popup, and then close the popup but it seems to work intermittently for me. The page launched by the form is a PHP page that modifies a record in the database. I thought that once the request is sent the PHP page will execute, even if the launching window is closed. Apparently not though. Sometimes the table is updated, sometimes it isn't. It seems like if the SQL operation isn't fast enough the page will be closed and the process is killed.
Here's the code:
<form id="xlationform" action="updatexlation.php" method="post" onsubmit="return closeForm(this);">
Source: <br>
<textarea disabled name="sterm" rows=10 cols=50><?php echo $source ?></textarea><br><br>
Translation: <br>
<textarea name="xlt" rows=10 cols=50><?php echo $xlation ?></textarea><br><br>
<input type="hidden" name="id" value="<?php echo $termid ?>">
<input type="submit" value="Update">
</form>
<script>
function closeForm(f) {
f.submit();
window.close();
}
</script>
What's the best way of working this out? I want the window to be closed but the DB operation needs to complete first and I don't want to query the DB again if possible. Thanks for your help.
Do it in updatexlation.php like :
$e = mysqli_query(...) if ($e) { echo "<script>window.close();</script>"; }
But as mentioned above, avoid using popups.
The response page should simply have window.close in the onload event.
I need to submit form data in a _blank window to a URL that contains a couple different question marks in this format:
http://first--sample--url.com?http://second--sample--url.com?a=someUserInput&b=moreUserInput
The first URL does not allow requests sent with the POST method. Trying a form submission using a standard GET method, the URL is truncated after the first question mark, and I can't move the second URL to a hidden form field since it is not a name/value pair.
When testing the below code on the iOS6 simulator, it works fine -- both the submit button and the iOS softkey "Go" button (that appears on the soft keyboard while inputting text) execute the window.open call as intended.
Testing on iOS7 devices however, the softkey "Go" button does not allow the window.open to happen, even though the submit button works:
<form onsubmit="clickThrough(); return false;">
<input type="text" name="a" id="a">
<input type="text" name="b" id="b">
<input type="submit">
</form>
<script>
var url = "http://first--sample--url.com?http://second--sample--url.com?a={a}&b={b}",
a = document.getElementById('a'),
b = document.getElementById('b');
function clickThrough() {
var clickURL = url.split("{a}").join(a.value).split("{b}").join(b.value);
window.open(clickURL, "_blank");
}
</script>
Test the fiddle here on iOS7: http://jsfiddle.net/BC2Vv/5/embedded/result/
And edit the fiddle here: http://jsfiddle.net/BC2Vv/5/
Does anyone know a workaround for this in iOS7 so the softkey "Go" button will allow window.open to complete, or do you have any other solutions?
Thanks in advance!