Consider the following HTML snippet containing some javascript utilizing prompt and unload. The prompt() method works fine but I want alerting something like Goodbye, user when reloading or leaving the page. Any help is greatly appreciated.
<html>
<body onload="promptName()" >
<script type="text/javascript">
function promptName()
{
var userName = prompt("What's your name ?", "")
return userName;
}
function goodBye()
{
alert("Goodbye, " + promptName() + "!");
}
window.onunload = goodBye;
window.onbeforeunload = goodBye;
</script>
</body>
</html>
try this
<script type="text/javascript">
var userName;
function promptName()
{
userName = prompt("What's your name ?", "")
return userName;
}
function goodBye()
{
//alert("Goodbye, " + promptName() + "!");
return("Goodbye, " + userName + "!");
}
window.onunload = goodBye;
window.onbeforeunload = goodBye;
</script>
stackoverflow
Related
I'm making a dynamic website that need to show the user's name in the heading. The heading should say 'Hello, ', when the user enters their name into the text box and presses the button. And the name has to stay on the page even if I refresh the page.
What will be the javascript functions and html codes to implement this?
However, to implement this, the site need to read the query string when the page is loaded, not when the button is pressed. How can I write functions for the onload event for this?
I've tried this, but this doesn't work.
function setUserName() {
var userName = document.getElementById("userName").value;
localStorage.setItem("userName", userName);
}
function getUserName() {
var userName = localStorage.getItem("userName");
return userName;
}
function displayUserName() {
var userName = getUserName();
if (userName) {
document.write("Hello, " + userName);
} else {
document.write("Hello, world!");
}
}
<input type="text" id="userName">
<button onclick="setUserName()">Set Name</button>
<body onload="displayUserName()"></body>
Try this code, It might helpfull to you
function setUserName() {
var userName = document.getElementById("userName").value;
localStorage.setItem("userName", userName);
location.reload();
}
function getUserName() {
var userName = localStorage.getItem("userName");
return userName;
}
function displayUserName() {
var userName = getUserName();
if (userName) {
document.getElementById("helloText").textContent = "Hello, " + userName;
} else {
document.getElementById("helloText").textContent = "Hello, world";
}
}
<span id="helloText"></span>
<input type="text" id="userName" name="userName">
<button onclick="setUserName()">Set Name</button>
<body onload="displayUserName()">
</body>
Local storage wont work with the snippet by the way
In my website I have a whatsapp sharing button, so i want to send my website link with a parameter,
For e.g.
abcd.com/?name=userinput
This is the code of whatsapp sharing button.
<script type="text/javascript">
var userinput=prompt("Please Enter Your Name");
</script>
<script><a href="whatsapp://send?text= Link abcd.html?name="><b>share on whatsapp</b></script>
You can do this:
JS:
<script>
//Ask the user for name and store it on name variable
var name = prompt("Please Enter Your Name");
//This will be called when the link is clicked
function sendWhatsapp(){
var url = "www.abcd.com/?name=" + name;
var sMsg = encodeURIComponent( "hi this is " + name + " and my link is " + url );
var whatsapp_url = "whatsapp://send?text=" + sMsg;
window.location.href = whatsapp_url;
}
</script>
HTML:
<a onclick="sendWhatsapp()">share on whatsapp</a>
FULL HTML CODE:
<html>
<head>
<script>
//Ask the user for name and store it on name variable
var name = prompt("Please Enter Your Name");
//This will be called when the link is clicked
function sendWhatsapp(){
var url = "www.abcd.com/?name=" + name;
var sMsg = encodeURIComponent( "hi this is " + name + " and my link is " + url );
var whatsapp_url = "whatsapp://send?text=" + sMsg;
window.location.href = whatsapp_url;
}
</script>
</head>
<body>
<a onclick="sendWhatsapp()">share on whatsapp</a>
</body>
</html>
var yourName; //global variable accessible to all functions
function showAnotherMessage() {
alert("Hi " + yourName ".\nThis is an alert message is no longer defined\nin the HTML but in a JavaScript file");
}
function init() {
yourName = Prompt("Hi. Enter your name.\nWhen the browser window is first loaded\nthe function containing this prompt window is called.", "Your name");
clickme = document.getElementById("clickme");
clickme.onclick = showAnotherMessage;
}
window.onload = init();
function showAnotherMessage() {
alert("Hi " + yourName + ".\nThis is an alert message is no longer defined\nin the HTML but in a JavaScript file");
}
you miss a "+" in your alert.
You are missing a + in your alert concatenation.
alert("Hi " + yourName + ".\nThis is an alert message is no longer defined\nin the HTML but in a JavaScript file");
Pass to the window.onload not the result of the function, but the reference of the function and add missed + in the alert's message
alert("Hi " + yourName + ".\nThis is an alert message is no longer defined\nin the HTML but in a JavaScript file");
Reference
window.onload = init;
You are missing a concatenation.
Prompt should be in lower case.
var yourName; //global variable accessible to all functions
function showAnotherMessage() {
alert("Hi " + yourName + ".\nThis is an alert message is no longer defined\nin the HTML but in a JavaScript file");
}
function init() {
yourName = prompt("Hi. Enter your name.\nWhen the browser window is first loaded\nthe function containing this prompt window is called.", "Your name");
clickme = document.getElementById("clickme");
clickme.onclick = showAnotherMessage;
}
window.onload = init();
if the issue is still there,, then it is related to calling the function
clickme.onclick = showAnotherMessage;
// instead use below
clickme.onclick = showAnotherMessage();
I tried this with umpteen examples we see on the net. But I guess there is none that is simple and works on all browsers (IE 8 and above as well).
I am trying to simply open up Outlook window with mailto link.
Email
JQuery:
$(function () {
$('#emailLink').on('click', function (event) {
alert("Huh");
var email = 'test#theearth.com';
var subject = 'Circle Around';
var emailBody = 'Some blah';
window.location = 'mailto:' + email + '?subject=' + subject + '&body=' + emailBody;
});
});
Granted, I am a jQuery newbie. The above just doesn't work. It just flickers the browser but nothing opens. I guess this is because of window.location.
Is there a simple solution? I want this to work in IE8 & above and in all browsers.
The body is generated automatically (in JSP).
here's working solution:
Email
and the function:
$(function () {
$('#emailLink').on('click', function (event) {
event.preventDefault();
alert("Huh");
var email = 'test#theearth.com';
var subject = 'Circle Around';
var emailBody = 'Some blah';
window.location = 'mailto:' + email + '?subject=' + subject + '&body=' + emailBody;
});
});
If you do not need the address as a text anywhere on the website I would suggest this:
$('a[data-mail]').on('click', function() {
window.location = 'mailto:' + $(this).data('mail')+'#yourdomain.net' + '?subject=Spotflow';
});
The link woud look like this:
Send me a mail
No chance for bots!
$(function () {
$('[name=emailLink]').click(function () {
var email = 'test#theearth.com';
var subject = 'Circle Around';
var emailBody = 'Some blah';
$(this).attr('href', 'mailto:' + email +
'?subject=' + subject + '&body=' + emailBody);
});
});
.click can be replaced with .mousedown and so on.. or just
$(function () {
$('[name=emailLink]').each(function() {
var email = 'test#theearth.com';
var subject = 'Circle Around';
var emailBody = 'Some blah';
$(this).attr('href', 'mailto:' + email +
'?subject=' + subject + '&body=' + emailBody);
});
});
Your selector is looking for an ID
$('#emailLink')
But you have only specified the name.
Add id="emaillink" to the anchor tag.
You don't need any javascript/jQuery at all for this, just the following HTML should do:
Email
There is a function which send an email via default email client. In body of email message, there are few tabs between words. But, when I send this message to default email client, these tabs are not present.
JavaScript Code:
function SendMail() {
try {
var mailAddress = 'test#gmail.com';
var mailSubject = 'Mail Subject';
var mailBody = 'Text goes here';
location.href = 'mailto:' + encodeURIComponent(mailAddress) +
'?subject=' + encodeURIComponent(mailSubject) +
'&body=' + encodeURIComponent(mailBody);
}
catch (err) { alert(err);}
}
Test Output: Text goes here
But, there should be tab instead of space between words.
Anybody, help me what's wrong with this ?
As you can see here: http://jsfiddle.net/VXL83/ the tabs are in the url, but with my setttings (Thunderbird, HTML email) they are eliminated from the body text by thunderbird.
function SendMail() {
try {
var mailAddress = 'test#gmail.com';
var mailSubject = 'Mail Subject';
var mailBody = 'Text\tgoes\there';
var url = 'mailto:' + encodeURIComponent(mailAddress) +
'?subject=' + encodeURIComponent(mailSubject) +
'&body=' + encodeURIComponent(mailBody);
alert(url);
location.href = url;
}
catch (err) { alert(err);}
}
SendMail();