how to write user input in whatsapp sharing message - javascript

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>

Related

JavaScript searching for a user based on their username?

I've created a function that uses getJSON to retrieve a data set found on an API website of registered Github Users
<!DOCTYPE html>
<!---->
<html>
<head>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
$(function(){
var user = $('#search').val();
$.getJSON("https://api.github.com/users/" + user)
.done(function(data) {
var br = "<br>";
var p = $("<p id='users'></p>");
var name = "Username: "+ user.login + br;
var pic = "Avatar Picture:" + br + "<img src='"+user.avatar_url+"'/>" +br;
var homeURl = "Homepage URL: "+"<a href='"+user.html_url+"'>"+user.html_url+"</a>" +br;
var location = "Location: "+"Null" +br;
var admin = "Admin: "+user.site_admin;
p.append("<p>"+ name + pic + homeURl + location + admin +"</p>");
$("#results").empty().append(p);
})
.fail(function(jqXHR) {
console.log("Error: " + jqXHR.status);
})
.always(function() {
console.log("Random Users Request finished");
});
});
</script>
</head>
<body>
<input id="search" type = "text">
<button>Search</button>
<div id="results"></div>
</body>
</html>
As you can tell I have began to modify it so that instead of displaying all users it only displayers the user that has been searched
var user = $('#search').val();
$.getJSON("https://api.github.com/users/" + user)
This snippet of code grabes the username entered in the text area and passes it to the getJSON method in the Url. An example URL is "https://api.github.com/users/mojombo", thus if the user enters "mojombo" then their profile would appear
This function is accessed via a button
<input id="search" type = "text">
<button>Search</button>
However when you search for the example user, in fact any user no data is displayed and a blank screen remains
You have not connected your function to the button.
As it stands, it runs before the user has typed anything into the search field (thus requesting "https://api.github.com/users/" because user at that time is an empty string) , and won't react at all to a button click.
Instead of just
$(function(){
/* code */
});
do
$(function(){
$('button').on('click', function(){
/* code that runs when button is clicked */
})
});

printing the contents of list in java

Trying write javascript that prints "username=john01" and "password=password123" from the url below. Currently it prints nothing...
index.html?username=john01&password=password123&lt=_c1F9F2B16-96B3-9D7B-CC19-D22A43D4FCA1_kA54D6D0E-881B-2792-22D3-B857150B1EFC&_eventId=submit&submit=Sign+In
<html>
<body>
<p> This code is for educational purposes only, and is not to be used with malicious intent. </p>
<script>
string = window.location.search;
content = string.split("");
usernameStart = 1;
usernameEnd = content.indexOf("&",0);
username = content.substring(usernameStart,usernameEnd));
document.write("Your username is " + username);
</script>
</body>
</html>
var url = 'index.html?username=john01&password=password123&lt=_c1F9F2B16-96B3-9D7B-CC19-D22A43D4FCA1_kA54D6D0E';
var username = url.match(/username=([^&]*)/)[1];
var password = url.match(/password=([^&]*)/)[1];
document.getElementById('userinfo').innerHTML = 'Your username is ' + username + '<br/>And your password is ' + password;
You can try it directly on my JSFiddle
If you replace 'url' with 'window.location' and remove the first line, it should work from the url.

Turn a javascript to JSON

I am working on a task where the customer enters their contact information, and then all the details will appear on an alert box, I am facing a problem with converting a javascript code to JSON
<script type="text/language">
var
function getinfo() {
try{ var firstName =
document.myForm.firstName.value;
var LastName =
document.myForm.LastName.value;
FullName= firstName + LastName;
var Gender =
document.myForm.Gender.value;
var mail =
document.myForm.mail.value;
var Telephone =
document.myForm.Telephone.value;
var MobilePhone =
document.myForm.MobilePhone.value;
alert("FullName : "+FullName +"<br/> Mail: " + mail + "<br/>Telephone:" + Telphone +"<br/>Mobile: " + MobilePhone);
}catch(err){alert('Exception :: '+err)}
}
</script>
Your script has some errors:
it's document.forms.myForm and so on.
you didn't define "FullName" as a variable.
you use "Telphone" in the alertbox instead of "Telephone"
I've corrected them here:
<script type="text/language">
document.forms.myForm.elements.sub.onclick= function()
{
try{ var firstName = document.forms.myForm.firstName.value;
var LastName = document.forms.myForm.lastName.value;
var FullName= firstName + LastName;
var Gender =document.forms.myForm.Gender.value;
var mail = document.forms.myForm.mail.value;
var Telephone = document.forms.myForm.Telephone.value;
var MobilePhone =document.forms.myForm.MobilePhone.value;
alert("FullName : "+FullName +"<br/> Mail: " + mail + "<br/>Telephone:" + Telephone +"<br/>Mobile: " + MobilePhone);
}catch(err){alert('Exception :: '+err)}
}
</script>
But your script hasn't got to do anything with converting something to JSON.
You might use JSON.stringify() to "JSONify" your variables.

Javascript unonload and onload function

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

jQuery mailto with anchor element

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

Categories

Resources