javascript if() dont work - javascript

I have PHP and JS script for uploading image. PHP file returns a var err:type and I'm checking in JS if return == err:type, but it doesn't work.
$(document).ready
(
function()
{
$('#avatar_image_upload_form').submit
(
function()
{
$('div#avatar_ajax_upload_demo img').attr('src','../../Files/Border/loading.gif');
}
);
$('iframe[name=avatar_upload_to]').load(
function()
{
var result = $(this).contents().text();
if(result !='')
{
$('div#avatar_ajax_upload_demo img').attr('src',result);
if(result == 'err:size')
{
$('div#avatar_ajax_upload_demo img').attr('src','../../Files/Border/avatar_big.jpg');
}
if (result == 'err:type')
{
$('div#avatar_ajax_upload_demo img').attr('src','../../Files/Border/avatar_invalid.jpg');
}
}
}
);
}
);
if(result == 'err:type') doesn't work, but result = "err:type"

According to this image:
There are a lot of white lines at the beginning of the string. You need to trim the result string to remove them:
var result = $(this).contents().text().trim();
You should best fix your PHP code in order not to send those blank lines.

[WRONG]
Maybe your error is here : (if avatar_upload_to isn't a variable)
$('iframe[name=avatar_upload_to]').load(
Should be
$('iframe[name="avatar_upload_to"]').load(
=====
[TEST]
What appends if you make this :
$('iframe[name=avatar_upload_to]').load(
function()
{
var result = $(this).contents().text();
console.log(result);
//or
alert(':'+result+':');
if(result !='')
{
$('div#avatar_ajax_upload_demo img').attr('src',result);
if(result == 'err:size')
{
$('div#avatar_ajax_upload_demo img').attr('src','../../Files/Border/avatar_big.jpg');
}
if (result == 'err:type')
{
$('div#avatar_ajax_upload_demo img').attr('src','../../Files/Border/avatar_invalid.jpg');
}
}
}
);

Related

how to get js context from htmlparser2 directly?

how to get js context from htmlparser2 directly?
var parser = new htmlparser.Parser({
onopentag: function(name, attribs){
if(name === "script" && attribs.type === "text/javascript"){
//console.log(text);
//console.log("JS! Hooray!");
}
},
ontext: function(text){
//fs.writeFileSync("../output/test.js",text.toString());
console.log(text);
},
onclosetag: function(tagname){
if(tagname === "script"){
//console.log("That's it?!");
}
}
}, {decodeEntities: true});
var input_src = fs.readFileSync('../input/test2.js', 'utf-8');
parser.write(input_src);
parser.end();
I try to get js from htmlparser2, but the content is not i am want.The content will include some other information expect the js. why the code do not work well?Could you give me some good ways to solve it?
You are not doing anything to limit the ontext function to only reading scripts. You need to set a flag whenever you encounter an opening script tag:
var inScriptElement = false;
var parser = new htmlparser.Parser({
onopentag: function(name, attribs){
if(name === "script" && attribs.type === "text/javascript"){
inScriptElement = true;
//console.log(text);
//console.log("JS! Hooray!");
}
},
ontext: function(text){
if (inScriptElement) {
//fs.writeFileSync("../output/test.js",text.toString());
console.log(text);
}
},
onclosetag: function(tagname){
if(tagname === "script"){
inScriptElement = false;
//console.log("That's it?!");
}
}
}, {decodeEntities: true});

Login using Json

I am trying to develop a login process using json.
My problem is that when I make many login attempts , parameters are not overriden but concatenated.
Below is the code I'm writing.
I do not see where the problem is.
Thank you in advance .
var loginReq = Titanium.Network.createHTTPClient();
loginReq.onload = function() {
var json = this.responseText;
Titanium.API.info("step 1 done");
var response = JSON.parse(json);
if (response.status == true) {
Titanium.App.Properties.setString("key", response.key);
alert("Success");
} else {
alert("Email or password wrong");
}
};
function doConnVerif() {
if ($.email.value != '' && $.password.value != '') {
loginReq.open("POST", "URL");
loginReq.send({
'email' : $.email.value ,
'password' : $.password.value
});
} else {
alert("Enter email and password");
}
}
By your question I can't grasp the full aspect of the problem, but definitely a good idea would be only allow one login attempt at a time.
Something like:
var pendingRequest = false;
// ...
if(!pendingRequest) {
loginReq.send({
'email' : $.email.value ,
'password' : $.password.value
});
pendingRequest = true
}
// ...
loginReq.onload = function() {
pendingRequest = false
// ...

Removing %20 from fullname Aweber

Passing variables from Aweber to wordpress Thank you page - However the name appears as follows:
firstname%20Lastname
code used
<script type="text/javascript">
var formData = function() { var query_string = (location.search) ? ((location.search.indexOf('#') != -1) ? location.search.substring(1, location.search.indexOf('#')) : location.search.substring(1)) : '';
var elements = [];
if(query_string) {
var pairs = query_string.split("&");
for(i in pairs) {
if (typeof pairs[i] == 'string') {
var tmp = pairs[i].split("=");
var queryKey = unescape(tmp[0]);
queryKey = (queryKey.charAt(0) == 'c') ? queryKey.replace(/s/g, "_") : queryKey;
elements[queryKey] = unescape(tmp[1]);
}
}
}
return {
display: function(key) {
if(elements[key]) {
document.write(elements[key]);
}
else {
document.write("<!--If desired, replace everything between these quotes with a default in case there is no data in the query string.-->");
}
}
}
}
(); </script>
then
<script>// <![CDATA[
formData.display('fullname')
// ]]></script>
tried
decodeURI(formData.display("fullname"))
But it doesn't work???
I've searched and searched and cant figure it out - Please anyone help???
Thanks.
Im guessing its part of a Wordpress plugin, so you just want a fix right :)
Try replacing this part:
return {
display: function(key) {
if(elements[key]) {
document.write(elements[key]);
} else {
document.write("<!--If desired, replace everything between these quotes with a default in case there is no data in the query string.-->");
}
}
}
With this:
return {
display: function(key) {
if(elements[key]) {
document.write(decodeURIComponent(elements[key]));
} else {
document.write("<!--If desired, replace everything between these quotes with a default in case there is no data in the query string.-->");
}
}
}

Check if var is set or exists in Angular controller

I have this function in a angular controller:
$scope.sendCompanyData = function() {
delete $scope.company["step1Form"];
delete $scope.company["step2Form"];
delete $scope.standard_address["state"];
$http.post(Routing.generate('create-company'), {
'company': $scope.company,
'standard_address': $scope.standard_address,
'phone': $scope.phone,
'courrier_address': $scope.courrier_address,
'logoFileName': $scope.logoFileName,
'mercantilDocFileName': $scope.mercantilFileName,
'rifDocFileName': $scope.rifFileName,
'standardAddressState': $scope.state.standard_address,
'standardAddressCity': $scope.city.standard_address,
'courrierState': $scope.courrierState.courrier_address,
'courrierCity': $scope.courrierCity.courrier_address
}).success(function(data) {
if (!data.success) {
if (!data.exception) {
$scope.errors = data.errors;
} else {
$scope.errors = data.exception;
}
} else {
$templateCache.removeAll();
ClientUser.loginToCompany(data.companyId);
if ($scope.mercantilFileName != "" && $scope.rifFileName != "") {
$noty.success(Translator.trans('company.register_success'));
} else {
$noty.success(Translator.trans('company.register_document_missing'));
}
$location.path('/empresa/' + data.companyAlias);
}
}).error(function(data, status) {
$scope.error = status;
});
$scope = angular.element($(".seller-layout.new")).scope();
$scope.section = 'segundo-paso';
}
The problem related to this code is on this line $scope.mercantilFileName != "" && $scope.rifFileName != "" since this is not checked or never happen even if I don't send any value on that. I'm getting crazy with this and perhaps it's easy but the code always goes trough else sentence. Any help on this?
Why not simply:
if ($scope.mercantilFileName && $scope.rifFileName) {...}
I think that $scope.mercantilFileNam is rather 'undefined' than an empty string("").

setInterval not processing properly

What I am trying to do is check my CodeIgniter every 2 seconds to see if the session is still alive. But for some reason when I use the following code I get this error:
"Uncaught SyntaxError: Unexpected Token ;"
Any ideas as to why this is happening?
Javascript:
setInterval(function() {
$.getJSON("<?=base_url()?>index.php/regUserDash/sessionExpire"), function(data) {
var sessionState = jQuery.parseJSON('{"sessionExpired":"true","sessionExpired":"false"}');
if(sessionState.sessionExpired === "true") {
alert('expired');
} else if(sessionState.sessionExpired == "false") {
alert('not expired');
}
});
}, 2000);
Ci code:
public function sessionExpire() {
if ($this->session->userdata("logged") == "1") {
echo json_encode(array("sessionExpired" => false));
} elseif($this->session->userdata("logged") == "0") {
echo json_encode(array("sessionExpire" => true));
}
}
You had an extra parenthesis here:
$.getJSON("<?=base_url()?>index.php/regUserDash/sessionExpire") // <--
This should work:
setInterval(function() {
$.getJSON("<?=base_url()?>index.php/regUserDash/sessionExpire", function(data) {
var sessionState = $.parseJSON('{"sessionExpired":"true","sessionExpired":"false"}');
if (sessionState.sessionExpired === "true") {
alert('expired');
} else if (sessionState.sessionExpired == "false") {
alert('not expired');
}
});
}, 2000);​

Categories

Resources