why does not onload() work when i close the tab? - javascript

i wrote this simple function to show a message when you close the window but it didnt work.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body onunload="closing()">
<script language="javascript">
function closing(){
alert('Bye');
}
</script>
</body>
</html>

Your popup blocker might block it. MDN docs source
That sort of behavior - alerts popping up on a close, are usually not desired by users.
Did you try using the unload event?

Related

When i execute an if condition with the continue keyword in a while loop my browser dosen't open

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<head>LOOPS</head>
<h1 id="string"></h1>
<script language="javascript" type="text/javascript">
let w = 10;
while(w<=15){
console.log(w)
if(w==13){
continue;
}
w++
};
</script>
</body>
</html>
So...As you can see here, Whenever i live reload this on my browser(firefox, chrome) It just keeps on reloading and dosen't open but when i use the for loop and then use the if condition to skip a loop, It opens.
Please is there something i'm doing wrong, Because i just started learning javascript.
It's because you're creating an infinite loop. When you have the if statement and the continue keyword, it will skip to the next loop without incrementing w variable.

prompt loads before anything else

hay if anybody understand why is prompt showing up even before the console printing
i appreciate the help
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>promptheadache</title>
</head>
<body>
<script>
console.log('i should appear first on the console')
var promptInput = prompt('I\'m am here befor anything else')
</script>
</body>
</html>
console.logs are actually asynchronous in that they are synchronized (as in always in order) but perform an asynchronous call.
prompt is a very old API and one of the few that actually blocks the page - so even-though it appears after the console.log you see it as soon as it is called.
That said - this is not true for every console nor is it guaranteed - the console.log may appear before the prompt depending on the browser/console implementation.
If you want to get the console before prompt you can add setTimeOut-setTimeout() is an asynchronous function.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>promptheadache</title>
</head>
<body>
<script>
console.log('i should appear first on the console')
setTimeout(() => {var promptInput = prompt('I\'m am here befor anything else')
},5000)
</script>
</body>
</html>

Why I can't exit the Fullscreen using esitFullscreen method?

Whenever I try to exit the Fullscreen in the console it shows that exitFullscreen is not a function
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
<button onclick="o()">O</button>
<button onclick="x()">X</button>
<script type="text/javascript" charset="utf-8">
function o(){
document.documentElement.requestFullscreen();
}
function x(){
document.documentElement.exitFullscreen();
}
</script>
</body>
</html>
From the code above by clicking on button x I should exit Fullscreen but it is not happening
Look at the documentation.
requestFullscreen is a method on elements
exitFullscreen is a method on the document
use this for exit the full screen
document.exitFullscreen();

console.log issue in a local folder

I just wonder what I am doing with console.log is wrong or not.
I have simple two files as below:
index.html
index.js
and when opening the index.html in chrome(c:\temp\index.html), it does not output console.log message in console tab as below.
So am I missing something?
As you can see, if you run it below code, it shows console.log properly.
function doSomething() {
console.log("Work!");
}
doSomething();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div>Hi</div>
<script scr='index.js'> </script>
</body>
</html>
Looks like you have a typo:
<script scr='index.js'>
should be
<script src='index.js'>
function doSomething() {
console.log("Work!");
}
doSomething();
<div>Hi</div>
<script src='index.js'> </script>
You have to change scr to src in that way it will works.

simple mailto function that composing an email from html file

I need to create a simple mailto function so when I click on the html file it brings up my default email application with the composed mail "ready-to-go." I figured a click event on the mailto ID would be sufficient, but it is not firing. What should I do? FYI, I do not want the user to click on the hyperlink. I plan on removing it.
<script type="text/javascript">
$(document).ready(function(){
$("#mymailto").click();
}); // document.ready
</script>
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE>Your Title Here</TITLE>
<META http-equiv="X-UA-Compatible" content="IE=edge">
<META http-equiv='cache-control' content='no-cache'>
<META http-equiv='expires' content='0'>
<META http-equiv='pragma' content='no-cache'>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</HEAD>
<BODY>
<a href="mailto:testEmail#gmail.com" id="mymailto" >email</a>
</BODY>
</HTML>
Just use document.location.href="mailto:testEmail#gmail.com";.

Categories

Resources