Turn a javascript to JSON - javascript

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.

Related

Check if file exists by checking two different naming conventions using jQuery/JS

I am trying to to build out profile pages for multiple users which include personalized photos/items that are stored are on a server. These items are labeled/named using the users' name. A user has a FirstName & LastName, with the option of using a PreferredName.
Initially, the items were named using PreferredName over Firstname if a Preferred Name existed. (ex: Fname: Robert; Lname: Smith; Pname: Bobby; FileName = SmithBobby.file)
Unfortunately, the user now has the ability to change their name on their profile from PreferredName back to FirstName, leading to a large portion of profiles to look for the incorrect file (item is actually named SmithBobby.file, while the profile is looking for SmithRobert.file.)
This being said, I would like to check for the item using both naming conventions (FirstLast.file & PreferredLast.file), if neither exists, it should default to use the default/generic photo. (default.file)
The below example is how I currently check to see if the user has a CV and profile picture on file. If no CV exists, it removes the element from the page. if the image doesn't exist, it defaults to default.jpg.
if (($.PageData.PreferredName == "") || ($.PageData.PreferredName == null)) {
$("#Name").text($.PageData.FirstName + " " + $.PageData.LastName);
document.title = ($.PageData.FirstName + " " + $.PageData.LastName + " | Profile");
$("#BioPageTitle").text($.PageData.FirstName + " " + $.PageData.LastName);
} else {
$("#Name").text($.PageData.PreferredName + " " + $.PageData.LastName);
document.title = ($.PageData.PreferredName + " " + $.PageData.LastName + " | Profile");
$("#BioPageTitle").text($.PageData.PreferredName + " " + $.PageData.LastName);
}
//FILENAME BUILD
var file_name = ($.PageData.LastName + $.PageData.FirstName);
var second_fname = ($.PageData.LastName + $.PageData.PreferredName);
file_name = file_name.replace(/[^0-9a-z]/gi, '');
second_fname = second_fname.replace(/[^0-9a-z]/gi, '');
var vita = $('#Vita');
var vita_url = "vita/" + file_name + ".pdf";
var second_vitaURL = "vita/" + second_fname + ".pdf";
var VitaLink = $("<a>").attr({
href: vita_url,
target: '_blank'
}).html("<strong>Curriculum Vitae</strong>");
$.get(vita_url)
.done(function() {
vita.html(VitaLink);
}).fail(function() {
vita.remove();
});
/*PHOTO BUILD/CHECK */
//PROFILE PICTURE
var img = $('#ProfilePicture');
var default_url = "photos/default.jpg";
var img_url = "photos/" + file_name + ".jpg";
img.error(function() {
$(this).attr('src', default_url);
});
img.attr('src', img_url);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
You should be able to extend your current function that checks the first link by checking for the second one if the first one fails. If the second one fails too, then you can remove the entry.
$.get(vita_url)
.done(function() {
vita.html(VitaLink);
}).fail(function() {
$.get(second_vitaURL)
.done(function() {
//modify the VitaLink with the correct url, if this doesn't work make a separate vitaLink type variable
VitaLink.attr('href', second_vitaURL);
vita.html(VitaLink);
}).fail(function() {
vita.remove();
});
});

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.

Fixing Syntax Error, on my Script Editor

function sendEmails(e){
var UserName = e.values[3];
var UserEmail = e.values[5];
var date = e.values[0];
var pin = e.values[4];
var subject = "[LFR] Application Succeed!";
var bodymessage = "Hi" + UserName + "for sending in your job application."
<br>
<br> + "The following is your Purchase Log Pin"
<br> + pin;
var emailsent = "Email Sent";
var isEmailsent = e.values[6];
if (isEmailsent ! = emailsent);
MailApp.sendEmail(UserEmail, subject, bodymessage);
}
The error message always shows, "Illegally formed XML syntax. (line 18, file "Code"). Someone please help me to fix it!
change this
var bodymessage = "Hi" + UserName + "for sending in your job application."
<br>
<br> + "The following is your Purchase Log Pin"
<br> + pin;
to
var bodymessage = "Hi" + UserName + "for sending in your job application.<br> <br>The following is your Purchase Log Pin<br>" + pin;
bascially, your string is not formed correctly
Also,
remove ; after the if statement
if (isEmailsent ! = emailsent)
{
MailApp.sendEmail(UserEmail, subject, bodymessage);
}

Load javascript on page load using asp.net

I have some working code (jQuery/Javascript) that makes a call to an API and submits data to it. The same service then returns a success or failure message depending on whether the data was inserted into the API db. The below works flawlessly when loaded in the browser.
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
$(document).ready(function () {
var groupType = getParameterByName('group').trim();
if (groupType == 'm') {
groupId = 'ICM.RealLife.Mobile';
} else if (groupType == 'd') {
groupId = 'ICM.RealLife.Desktop';
}
var email = getParameterByName('email').trim();
var mobileTel = getParameterByName('mobile').trim();
var panelistId = mobileTel;
var password = 'icm001';
var locale = 'en';
alert('email=' + email + '\n\nMobile=' + mobileTel + '\n\nGroup=' + groupId);
if (mobileTel != '' && email != '' && groupId != '') {
//Build up querystring to pass to API
var dataString = "panelistId=" + (encodeURIComponent('+') + mobileTel) + "&groupId=" + groupId + "&emailAddress=" + email + "&password=" + password + "&locale=" + locale + "&mobileNumber=" + (encodeURIComponent('+') + mobileTel) + "";
//var apiResult;
//send to API
$.getJSON('https://www.analyzeme.net/api/server/prereg/?', dataString + '&callback=?', function (getResult) {
//apiResult = JSON.stringify(getResult);
//alert(apiResult);
});
//} else {
// alert('Incorrect parameters!');
}
});
I now have to get this working using a 1x1 tracking pixel using aspx like below;
<img src="http://www.somedomain.com/pixel.aspx?email=email#email.com&mobile=+441111222222&group=d" width="1" height="1"/>
BUT, I do not know how to get my JavaScript to fire in the asp.net page when it is hit? I know I need to do something with RegisterStartupScript but how do I get all that JS into it and how do I get it to fire when the page is hit. I know how to return an img/gif using response headers, so I am cool with that.
Help greatly appreciated! :)
Call the JS function from your Page_Load event in code behind. This will fire every time the page is loaded.
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(Page, GetType(), "myFunction", "myFunction();", true);
}
JavaScript
function myFunction() {
//Code you want to run from document.ready
}

How do I pull phoneNumber from Linkedin Javascript API array

I appreciate any help with those familiar with the LinkedIn JavaScript API. Question: How do I pull in the phone number and email from the field array. In my example, posted to a php script, both phoneNumber and emailAddress are undefined when set to $_POST[''];:
<script type="text/javascript">
//Runs when the JavaScript framework is loaded
function onLinkedInLoad() {
IN.UI.Authorize().params({"scope":["r_fullprofile", "r_emailaddress", "r_contactinfo"]}).place();
IN.Event.on(IN, "auth", onLinkedInAuth);
}
//Runs when the viewer has authenticated
function onLinkedInAuth() {
IN.API.Profile("me").fields("id,firstName,lastName,phoneNumbers,emailAddress,positions").result(displayProfiles);
}
// 2. Runs when the Profile() API call returns successfully
function displayProfiles(profiles) {
var p = profiles.values[0];
var phone = profiles.values[0].phoneNumbers;
for (var i in p.positions.values) {
var pos = p.positions.values[i];
if (pos.isCurrent)
var company = pos.company.name;
}
//AJAX call to pass back vars to server
var http = new XMLHttpRequest();
var postdata= "id=" + p.id + "&fName=" + p.firstName + "&lName=" + p.lastName + "&phone=" + phone + "&email1=" + p.emailAddress + "&company=" + company;
http.open("POST", "../inc/linkedin.php", true);
use fields like this http://developer.linkedin.com/documents/profile-fields
Try this...
IN.API.Profile("me")
.fields('id,email-address,first-name,last-name,date-of-birth,phone-numbers,positions,num-connections')
.result(displayProfiles)
.error(displayErrors);

Categories

Resources