Why My Javascript code does not open Message app in mobile phone? - javascript

I use this code to open Message app automatically:
function getMobileOperatingSystem() {
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
if (/windows phone/i.test(userAgent)) {
window.open('sms:+989123122223?body=s');
}
if (/android/i.test(userAgent)) {
window.open('sms:+989123122223?body=a');
}
if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
window.open('sms:+989123122223&body=b');
}
}
getMobileOperatingSystem();
<!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>
</body>
</html>
but it is only opening a blank page and nothing will happen. how can I fix my script and open Message app automatically?

Related

Java script google api to get authorization code

I done this code using tutorial but it does not work, it simply does open pop-up to enter your credentials for google. What is wrong with this code?
<!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>
<button id="sign-in-button">Sign in</button>
<script src="https://apis.google.com/js/api.js"></script>
<script>
gapi.auth2.init({
client_id: "655720894976-2rkkoov9efteqna8hq3vbc632brur2jf.apps.googleusercontent.com"
});
document.getElementById("sign-in-button").addEventListener("click", function() {
// Get an authorization code
gapi.auth2.getAuthInstance().grantOfflineAccess({
scope: "https://www.googleapis.com/auth/cloud-platform"
}).then(function(response) {
var authorizationCode = response.code;
});
});
</script>
</body>
</html>

Select event - Why is nothing displayed in the console?

Why is nothing displayed in the console when selecting text?
<!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>
dhksdh lsjkdlskdj s fjlkfjs d lsdjslkdj s sldjsldkj s
<script>
const onSelect = () => {
console.log('onSelect text')
}
document.body.addEventListener('select', onSelect)
</script>
</body>
</html>
Maybe I wrote something wrong?

HTML button onclick function not recognised in JavaScript

I have an HTML file linked to a script. A button defined in the HTML has an onclick function 'incrementLabel()' but the function in the script throws the warning:
'incrementLabel' is declared but its value is never read.
function incrementLabel() {
var text = document.getElementById("testLabel").textContent;
document.getElementById("testLabel").innerText = (parseInt(text) + 1);
}
<!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">
<link rel="stylesheet" href="styles.css">
<script src="js/Main.js"></script>
<title>Test Web App</title>
</head>
<body>
<button onclick="incrementLabel()">Test</button>
<label id="testLabel">0</label>
</body>
</html>
I can't find any solutions to the problem other than writing the function in a in HTML. Any help would be much appreciated.
Thanks!
<!DOCTYPE HTML>
<script>
function incrementLabel() {
var text = document.getElementById("testLabel").textContent;
document.getElementById("testLabel").innerText = (parseInt(text) + 1);
}
</script>
<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">
<link rel="stylesheet" href="styles.css">
<script src="js/Main.js"></script>
<title>Test Web App</title>
</head>
<body>
<button onclick="incrementLabel()">Test</button>
<label id="testLabel">0</label>
</body>
</html>
This works perfectly fine. Check if you are importing your JS script correctly.
You can put the javascript code into a before the closing tag .
HTML:
<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">
<link rel="stylesheet" href="styles.css">
<script src="js/Main.js"></script>
<title>Test Web App</title>
</head>
<body>
<button onclick="incrementLabel()">Test</button>
<label id="testLabel">0</label>
<script>
function incrementLabel() {
var text = document.getElementById("testLabel").textContent;
document.getElementById("testLabel").innerText = (parseInt(text) + 1);
}
</script>
</body>
Some of online tools like https://playcode.io don't recognise your code but I have tested in vscode and works fine.
Maybe have to use other online tool or can test if you have imported the code well with a console.log("Works fine") into your javascript file.
Sorry for my english.

How to close an html website using javascript?

So basically, I'm trying to create a system that closing the website when you get the password wrong. but I started to learn js just a couple of days ago and I don't know much of it yet.
here is the code:
<!DOCTYPE html>
<html id="web" 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>MySite</title>
<script>
var web = document.getElementById("web");
var password = "BigDips12";
alert("Hey, For security messures and to make sure you are truly not a robot ...");
var security = prompt("Please enter the password here:");
if (security === password) {
alert('Welcome!')
} else {
alert('Password is wrong! The program will close itself now..')
function closeWin() {
web.close();
}
}
</script>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
I really need help pls
To close the browser tab use
window.close();
And u wasn't calling the function also
<!DOCTYPE html>
<html id="web" 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>MySite</title>
<script>
function closeWin() {
window.close();
}
var web = document.getElementById("web");
var password = "BigDips12";
alert("Hey, For security messures and to make sure you are truly not a robot ...");
var security = prompt("Please enter the password here:");
if (security === password) {
alert('Welcome!')
} else {
alert('Password is wrong! The program will close itself now..')
closeWin()
}
</script>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>

Cordova notification alert not working on device ready or load but working onClick events

I am new with cordova app development and trying to show an alert message when app loads but currently confused why the cordova-plugin-dialogs does NOT fire on load but when I used onclick event it fires.
Here is my code
<!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>
<button onclick="checkPolicyConfirm()">Click Me</button>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script>
document.addEventListener("deviceready", onDeviceReady, false);
function alertRegiterDismissedNow() {}
function checkPolicyConfirm() {
navigator.notification.alert(
"Setting is not available as of now", // message
alertRegiterDismissedNow, // callback
"Test Notifications", // title
"OK" // buttonName
);
}
function onDeviceReady() {
checkPolicyConfirm();
}
</script>
</body>
</html>

Categories

Resources