$(function () {
//To send data from input text
$('#add').click(function () {
var name = $('#name').val();
var gender = $('#gender').val();
var age = $('#age').val();
var address = $('#address').val();
var illness = $('#illness').val();
var selectunit = $('#selectunit').val();
if (typeof name !== null) {
my codes here...
} else {
alert('Some fields are missing!');
}
return false;
});
});
i want to trap my variables (name,age,address,illness) not to be null before doing the codes
please help thanks
use
name != null || name != ""
instead of
typeof name !== null
if your case is to check if name is undefined then use
typeof name !== "undefined"
You can check for null like so:
if (somevariable == null) { /* code */ }
The check for undefined is a little different:
if (typeof somevariable === "undefined") { /* code */ }
Related
I have a javascript function which get's me the emailID of the respective text added.
But what happens if the EmailID doesn't exist's it gives me error as
Microsoft JScript runtime error: Object doesn't support this property or method
Here is the function.
function getEmailIdByType() {
StrPriFnName = "FunGetEmailIdByType(" + document.getElementById('TxtPartyName').value + ")";
var ObjPriXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP")
ObjPriXMLHTTP.open("GET", "FrmInwardXMLHTTP.aspx?para=" + StrPriFnName, false);
ObjPriXMLHTTP.send("");
if (Trim(ObjPriXMLHTTP.responseText) != "") {
var StrPriData = ObjPriXMLHTTP.responseText.Split('~');
document.getElementById('TxtEmail').value = StrPriData[1];
}
else {
return false;
}
}
It gives me error at line:-
var StrPriData = ObjPriXMLHTTP.responseText.Split('~');
if (Trim(ObjPriXMLHTTP.responseText) != "") {
var StrPriData = ObjPriXMLHTTP.responseText.Split('~');
document.getElementById('TxtEmail').value = typeof StrPriData[1] != "undefined" ? StrPriData[1] : "";
}
else {
return false;
}
You should try to access the responseText only when the ObjPriXMLHTTP changes it's readyState
ObjPriXMLHTTP.onreadystatechange = function() {
if (ObjPriXMLHTTP.readyState == 4 && ObjPriXMLHTTP.status == 200) {
var StrPriData = ObjPriXMLHTTP.responseText.split('~');
}
Probably the responseText property is only available after it got a response. The response hasn't really arrived where you try to see it.
Here is a full example.
I have following code where i combine some variables to create path to the another existing object and his attribute.
Problem is that i alway get only string, so i would like to "convert" it into the object.
// SET CUSTOM CONTENT FOR COLUMN IF CONTACT ATTR IS EXISTS
if(value.concatByFields != null) {
preparedGridColumnItem.template = function (responseData) {
var nameForConcat;
var fieldName;
var objectName;
var pathToReturn;
$.each(value.concatByFields, function( index, concatField ) {
nameForConcat = null;
fieldName = null;
objectName = null;
objectName = value.field;
fieldName = concatField.fieldName;
console.log("FIELD NAME IS");
console.log(JSON.stringify(fieldName));
console.log("OBJECT NAME IS");
console.log(objectName);
nameForConcat = objectName+"."+fieldName;
console.log("CONCATED NAME IS");
console.log(nameForConcat);
console.log("OBJECT ADDRESS IS FOLLOWING");
console.log("responseData."+nameForConcat);
pathToReturn = "responseData."+nameForConcat;
});
//TODO : IS ALWAYS RETURNED AS STRING
return pathToReturn;
};
}
Returned value should be value of another and global existing json object. But now is it always string.
It means:
responseData.SomeObject.surname
How can i solve it please?
Many thanks for any help.
if(value.concatByFields != null) {
preparedGridColumnItem.template = function (responseData) {
var fieldName;
var objectName;
var pathToReturn;
$.each(value.concatByFields, function( index, concatField ) {
objectName = value.field;
fieldName = concatField.fieldName;
pathToReturn = responseData[objectName][fieldName];
});
//TODO : IS ALWAYS RETURNED AS STRING
return pathToReturn;
};
}
I have a issue in getting response in Kony application. this is the code
function getServerResponceOption(url){
alert(url);
var httpclient2 = new kony.net.HttpRequest();
var requestMethod = constants.HTTP_METHOD_GET;
var async = true;
httpclient2.open(requestMethod, url,async);
if(getAccessToken() != null && getAccessToken() != ""){
httpclient2.setRequestHeader("AuthToken",getAccessToken());
}
httpclient2.send();
httpclient2.onReadyStateChange = HandleResponce(httpclient2);
}
function HandleResponce(obj)
{
alert("Getting data "+obj.readyState+" Status "+obj.status+" Response "+obj.response );
if(obj.readyState == 4 )
{
if (obj.response != null && obj.response != "")
{
var jsonObj = obj.response;
handleResponseOption(0,jsonObj);
return;
}
else
{
}
}else{
var state = obj.status;
alert("Readystate "+obj.readyState+" Status = "+state);
}
if (obj.response != null && obj.response != "")
{
var jsonObj = obj.response;
handleResponseOption(1,jsonObj);
}
}
Here i got server response if i put the alert message in HandleResponce(obj) without the alert i didn't get any response. the ready state is 1 and status is 0. What is the problem occurred if i remove the alert message?
Note: URL and getAccessToken() is getting values.
You are calling function in line, When you use HandleResponce(httpclient2) function is immediately executed.
httpclient2.onReadyStateChange = HandleResponce(httpclient2);
Change your code as
httpclient2.onReadyStateChange = function(){ HandleResponce(httpclient2) };
Is there a way to add a url param like: http://site.com?open=true
And on document ready, if jQuery sees the open param set to true execute a function?
Thanks
first lets make a good Query string searcher in JS
function querySt(qsName, url)
{
var theUrl;
if (url == null || url == undefined)
theUrl = window.location.search.substring(1); else theUrl = url;
var g = theUrl.split("&");
for (var i = 0; i < g.length; i++) {
var pair = g[i].split("=");
if (pair[0].toLowerCase() == qsName.toLowerCase())
{
return pair[1];
}
}
return null;
}
$(function (){
if (querySt("open")!='true') return;
});
taken from website http://www.onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/
function $_GET(q,s) {
s = s ? s : window.location.search;
var re = new RegExp('&'+q+'(?:=([^&]*))?(?=&|$)','i');
return (s=s.replace(/^?/,'&').match(re)) ? (typeof s[1] == 'undefined' ? '' : decodeURIComponent(s[1])) : undefined;
}
You can test location.href with a regex:
if (location.href.match(/open=true/)
// do something
You might want to work on the regex though, to make sure it works for you.
I try to use the jquery + json to get all elements in form and build a JSON var to post in a ASP.NET MVC method.
$.fn.serializeObject = function () {
var o = {};
var a = this.serializeArray();
$.each(a, function () {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
$("#btnPost").click(function () {
alert(JSON.stringify($("#frm").serializeObject())));
});
It method get all fields in a form and build a JSON, but it dont put JSON inside JSON.
Example:
If i have the follow form:
<input name="person.name"><input name="person.age"><input name="person.address.street">
The serialized string build a JSON like this
{ "person.name": "??", "person.age": "??", "person.address.street": "??" }
I need a plugin or some function to generate like this:
{ "person": { "name" : "??", "age" : "??", "address":{ "street": "??" } } }
Your problem is not "JSON within JSON" (which is a misnomer anyway - JSON supports nesting just fine), your problem is you've misinterpreted how this process works.
Your serializeObject() method is just reading the names - as strings There's nothing in javascript that makes this process "automagically" resolve the dot-notation for you - the periods are just treated as part of the property name.
You'll need to split the names on the periods and proceed accordingly. A little dab of recursion and you're there.
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each( a, function()
{
if ( /\./.test( this.name ) )
{
resolveProperty( o, this.name.split( '.' ), this.value );
} else {
o[this.name] = this.value;
}
} );
return o;
function resolveProperty( object, properties, value )
{
if ( properties.length > 1 )
{
var property = properties.shift();
if ( 'undefined' == typeof object[property] )
{
object[property] = {};
}
resolveProperty( object[property], properties, value );
} else {
object[properties.shift()] = value;
}
}
};