Catch refresh and change page to index.html - javascript

i have a question. I want to detect, if a user has pressed F5 or the refresh button. If so, i want to change the page to index.html. Here is my code:
$(window).on('beforeunload',function(){
$.mobile.changePage($(document.location.href="/index.html"),{
transition:"slide",
changeHash:false
});
});
But it's not working. Has anybody a solution, how i can achieve this?
Thanks.

Just get the key value:
$(document).keypress(function(e) {
if(e.which == 116) {
window.location.href = '/index.html';
}
});
Key Reference
Alternative method:
document.onkeydown = fkey;
document.onkeypress = fkey
document.onkeyup = fkey;
var wasPressed = false;
function fkey(e){
e = e || window.event;
if( wasPressed ) return;
if (e.keyCode == 116) {
window.location.href = '/index.html';
wasPressed = true;
}else {
alert("Window closed");
}
}

Related

checkSound() Function and Express interfering

I currently have a function set up in my global.js file that starts a song and pauses it if the 4 key is pressed. However, when I implement this function it causes my routing to stop working. So in my mainMenu.js file when you press the 1 key it should bring you to game.html but nothing happens. I'm not sure of what is causing this interference. Any ideas as to how I can stop this from happening
global.js:
window.onload = function(){
}
//music
var audio = new Audio('/music/oregonMusic.mp3')
function checkSound(){
audio.play();
document.onkeypress = function(e){
if(audio.paused){
audio.play();
}
else if(!audio.paused && e.keyCode === 52){
audio.pause();
}
}
}
checkSound();
mainMenu.js:
window.onload = function(){
//console.log('swing');
checkSound();
}
document.onkeypress = function(e){
//console.log('key pressed: ' + e.keyCode)
if(e.keyCode === 49){
window.location = 'game'
}
else if(e.keyCode === 51){
window.location = "topTen"
}
}
using
document.onkeypress = function(e) { ... }; // 1
document.onkeypress = function(e) { ... }; // 2
results in only function // 2 being called on keypress
instead, you should use addEventListener
so, global.js
document.addEventListener('keypress', function(e) {
if(audio.paused){
audio.play();
}
else if(!audio.paused && e.keyCode === 52){
audio.pause();
}
});
and mainMenu.js
document.addEventListener('keypress', function(e) {
if(e.keyCode === 49){
window.location = 'game'
}
else if(e.keyCode === 51){
window.location = "topTen"
}
});
now both handlers will be called

how detect CTRL+q in javascript

How detect ctrl+q with javascript, this is my code
<body>
<p id="x"></p>
<script>
window.onkeydown = function() {detect(event);}
window.onkeypress = function() {res(event);}
var act = false;
function detect(event) {
if(event.ctrlKey) {
act = true;
}
else
act = false;
}
function res(event) {
if(act) {
document.getElementById("x").innerHTML = "ctrl " + String.fromCharCode(event.which);
}
else
document.getElementById("x").innerHTML = String.fromCharCode(event.which);
}
</script>
</body>
I want do it with javascript only.
You can detect it using the following function:
document.addEventListener("keydown", function (event) {
event.stopPropagation();
event.preventDefault();
if(event.ctrlKey && event.keyCode == 81)
{
console.log("CTRL + Q was pressed!");
}
else
{
console.log("Something else was pressed.");
}
});
The stopPropagation() and preventDefault() calls prevent the browser's default behaviour from occurring.
If you want to detect other keys, this page is rather useful: http://asquare.net/javascript/tests/KeyCode.html

How to prevent Chrome open history after press CTRL+H

I mean is there any way to prevent the default accesskey in Chrome.
var text = document.getElementById("text");
text.onkeyup = function(e) {
if(e.ctrlKey && e.keyCode == 72) {
// do something...
alert("You wont see me cause Chrome will open history manager");
}
}
<textarea id="text"></textarea>
This should work. You need Keydown Event.
var text = document.getElementById("text");
text.addEventListener("keydown", function(e) {
console.log(e.keyCode);
if (e.keyCode == 72 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
e.preventDefault();
alert('Stopped');
}
}, false);
<textarea id="text"></textarea>
I've added isCtrlDown variable and keyup with keydown event, to achieve what you're looking for because I didn't see isKeyDown kind of function in Key as discussed here.
var isCtrlDown = false;
var text = document.getElementById("text");
text.onkeydown = function(e){
if(e.keyCode == 17){
isCtrlDown = true;
}
if(isCtrlDown && e.keyCode == 72){
// do something...
console.log("You wont see me cause Chrome will open history manager");
}
e.preventDefault();
}
text.onkeyup = function(e){
if(e.keyCode == 17){
isCtrlDown = false;
}
e.preventDefault();
}

wait till function execute completely to execute the other

I have this jquery code to handle some cases if user clicked right or left button but the navigate became a mess when the user click both buttons together
$(document).ready(function(){
$(document).keydown(function(e){
if (e.keyCode == 37) {
setTimeout(LeftKeyPressed(),5000);
}
else if(e.keyCode == 39){
setTimeout(RightKeyPressed(),5000);
}
});
});
I want to simply block any call till the first or second function completely executed
Use a flag to block the unnecessary function calls like this,
$(document).ready(function(){
var xKeyPressed = false;
$(document).keydown(function(e){
if (e.keyCode == 37 && !xKeyPressed) {
setTimeout(LeftKeyPressed(),5000);
}
else if(e.keyCode == 39 !xKeyPressed){
setTimeout(RightKeyPressed(),5000);
}
});
function LeftKeyPressed()
{
xKeyPressed = true;
//Your code
xKeyPressed = false;
}
function RightKeyPressed(){
xKeyPressed = true;
//Your code
xKeyPressed = false;
}
});
Set a simple check if your code is running LeftKeyPressed or RightKeyPressed like this:
var inFunction = false;
$(document).ready(function(){
$(document).keydown(function(e){
if (e.keyCode == 37 && !inFunction) {
inFunction = true;
setTimeout(LeftKeyPressed(),5000);
}
else if(e.keyCode == 39 && !inFunction){
inFunction = true;
setTimeout(RightKeyPressed(),5000);
}
});
});
//Add this in your functions, both Left and Right variants,
function LeftKeyPressed(){
//Do your normal stuff here
inFunction = false;
}
There is other ways of doing this as well, but this is in my opinion a clear and concise way of doing it. You can potentially get race conditions with this approach.
$(document).ready(function(){
var navt;
$(document).keydown(function(e){
if(navt != null) return;
if (e.keyCode == 37) {
navt = setTimeout(function(){ LeftKeyPressed(); navt = null; },5000);
}
else if(e.keyCode == 39){
navt = setTimeout(function(){ RightKeyPressed(); navt = null; },5000);
}
});
});

SyntaxError on "function x.y () {...}"?

How can I fix the following javascript error ? This is a handler for an ASP.NET page to disable postbacks when the enter key is pressed:
<script type="text/javascript">
function document.onkeydown() {
if (event.keyCode == 13) {
event.returnValue = false;
event.cancel = true;
}
}
</script>
Note that document.onkeydown is not a valid function name. You probably wanted to do this:
document.onkeydown = function(ev) {
if (ev.keyCode == 13) {
// ...
}
Or better:
document.addEventListener('keydown', function(ev) {
if (ev.keyCode == 13) {
// ...
});
To add to maerics answer, to get access to the event, you need it as an argument...
document.addEventListener( 'keydown', function( event ) {
console.log( event, event.keyCode );
if (event.keyCode == 13) {
event.returnValue = false;
event.cancel = true;
}
});

Categories