JavaScript variable not is undefined in IE9 - javascript

Hello I have next JS code (this code not my own)
function somename(f)
var procName = "";
var procParams = new Array();
var parseEl = "";
var parseEls = new Array();
var parseInd = 0;
var procParamsInd = 0;
var IsValRead = false;
for (i = 0; i < f.length; i++) {
if (f[i] == "(")
break;
else
procName = procName + f[i];
}
}
I will redo it to much better way to find data before "(", but i wounder why procName variable is always undefined in IE9 in all browsers all working well.

I have a vague recollection that at least some versions of IE do not support indexing to access string characters. Try using charAt instead; or a better algorithm. It is almost certainly the f[x] which is causing your undefined's.

A better way to get the substring before ( is this:
var f = "name(2,4,5)",
procName = f.slice(0, f.indexOf('(')); //slice from start until before "("
console.log(procName)​; //name

Related

HTML Logic Form Obfuscating Password

I'm attempting to troubleshoot a login issue on an old system that noone here is very familiar with. We have what we believe to be the admin password, but it isn't working. I'm just grasping, but I thought maybe a browser issue, considering how old the system is, so I tried using Postman to see what kind of response I get, which resulted in a failure.
However, I'm noticing now that they seem to be using some method to obfuscate the password, and I don't really understand what it's doing.
The login form method is this.
<form method="post" name='login' action="/?language=en" onsubmit="document.getElementById('submit').disabled = true; document.getElementById('pass').value = CJMaker.makeString(document.getElementById('pass').value);" >
and the CJMaker file contains this.
function CJMaker(e)
{}function _CJMaker_makeString(val)
{if (val == null)
{return val;}var size = val.length;var retVal = new String();var conVal = new String();for (var i = 0; i < size; i++)
{var current = val.charCodeAt(i);current = current^4;conVal += String.fromCharCode(current);}for (var i = size - 1; i >= 0; i--)
{retVal += conVal.charAt(i);}return retVal;}CJMaker.makeString = _CJMaker_makeString;
So it looks like it's just using char codes, and I suspect that the password in the database isn't the actual password, but instead is whatever this would create.
I'm afraid I just do not understand this well enough to reverse it. I'm sure it's simple to some of you javascript guys though.
Can anyone tell me more about what this is doing?
The function XORs the character code of each character with 4, and then reverses the result.
This function is its own inverse, so if you have an encoded password on the server, run the function on that to get what you need to type.
function CJMaker(e) {}
function _CJMaker_makeString(val) {
if (val == null) {
return val;
}
var size = val.length;
var retVal = new String();
var conVal = new String();
for (var i = 0; i < size; i++) {
var current = val.charCodeAt(i);
current = current ^ 4;
conVal += String.fromCharCode(current);
}
for (var i = size - 1; i >= 0; i--) {
retVal += conVal.charAt(i);
}
return retVal;
}
CJMaker.makeString = _CJMaker_makeString;
let instring = "abcdefgh";
let obfusc = CJMaker.makeString(instring);
let outstring = CJMaker.makeString(obfusc);
console.log(instring, obfusc, outstring);

How to build-in array sub-function in my setAttribute function?

I have written a function that automatically inserts an attribute (i.e. a per case adapted onclick function) in input elements. It also makes a few exceptions. It looks like this, somewhat simplified for clarity reasons:
function insertAttribute() {
var allInputs = document.getElementsByTagName('input');
var allInputsCount = allInputs.length;
var thatInput = null;
for (i = 0; i < allInputsCount; i++) {
thatInput = allInputs[i];
var highlightFunction = "highlightItem('"+thatInput.name+"-row','"+thatInput.name+"-button')";
if ((thatInput.name != "A") && (thatInput.name != "B") && (thatInput.name != "C"))
thatInput.setAttribute("onclick",highlightFunction);
}
}
The problem is, there are some 20 exceptions. I could expand the if line, but I would rather do it with an array. But how do I do that? I googled how to use array in javascript function, and the (two top) results suggest I should do it like this:
function insertAttribute() {
var allInputs = document.getElementsByTagName('input');
var allInputsCount = allInputs.length;
var thatInput = null;
for (i = 0; i < allInputsCount; i++) {
thatInput = allInputs[i];
var highlightFunction = "highlightItem('"+thatInput.name+"-row','"+thatInput.name+"-button')";
var exceptedArray = ["A","B","C"];
if (thatInput.name != exceptedArray)
thatInput.setAttribute("onclick",highlightFunction);
}
}
But that doesn't work -- the attribute is still inserted in the exceptions. How should it be done? I would need a vanilla script solution. I'll be happy with a good tutorial, too. As you might have guessed, this is the first time I'm using such an array sub-function.
The solution offered in the comment, exceptedArray.indexOf(thatInput.name)==-1 worked in most browsers, but not in IE8. Its script debugger said that it did not support indexOf. It does in other contexts, but apparently not in this context.
In the meantime, I learned how to make a script loop through an array myself. And this works in all browsers:
var allInputs = document.getElementsByTagName('input');
var allInputsCount = allInputs.length;
var thatInput = null;
for (var i=0; i<allInputsCount; i++) {
thatInput = allInputs[i];
var highlightFunction = "highlightItem('"+thatInput.name+"-row','"+thatInput.name+"-button')";
var exceptedNamesArray = ["A","B","C","A4dTInput","A4eTInput"];
var excNamesArrayCount = exceptedNamesArray.length;
var excName = null;
for (var j=0; j<excNamesArrayCount; j++) {
excName = exceptedNamesArray[j];
if (thatInput.name != excName)
thatInput.setAttribute("onclick",highlightFunction);
}
}

How to get the Last Value from Cookie with 2 variables inside Using split() methode in javascript

I have cookie that consist of 2 variables. I want to get the value from the variable #2 (the last one) But I don´t know how to deal with that.
From PHP
I have set cookie like this :
setcookie ('mysite'.$user_id,'user_id='. $user_id. '&msg_id='. $c_id, time() + $cookie_time);
What I'm trying to get is the value of msg_id
In javascript
var ca="mysite"+user_id;
ca = new Array();
ca = document.cookie.split(';');
for (var w in ca)
NmeVal = new Array();
NmeVal = ca[w].split('=');
But how can I get just only the value of msg_id as I mentioned above?
UPDATE
I got it to work now by doing this:
var NmeVal = new Array();
NmeVal = ca[w].split('=');
var vl =unescape(NmeVal[1]);
var gala = vl.split('=');
alert(gala[2]);
I get the only value I want::
it work in IE, FIrefox, Chrome
But why didn´t it work in Safari ?
UPDATE # FINAL
Finally I found the solution jquery.cookie.js
Only 3 lines work and the job is done !!!
Create - get - delete cookie like crazy and the plugin is not that big file like other packages.
And because of this plug in I start to use cookie like crazy now :)
You are better of storing information in cookies this way:
setcookie("user_id", $user_id);
setcookie("msg_id", $c_id);
// ...
Javascript function for getting the cookie value by giving a name:
var user_id = getCookie("user_id");
function getCookie(name) {
var result = "";
var myCookie = " " + document.cookie + ";";
var searchName = " " + name + "=";
var startOfCookie = myCookie.indexOf(searchName);
var endOfCookie;
if (startOfCookie != -1) {
startOfCookie += searchName.length;
endOfCookie = myCookie.indexOf(";", startOfCookie);
result = unescape(myCookie.substring(startOfCookie, endOfCookie));
}
return result;
}
When you are parsing a cookie, parse from the outside in.
One thing I found when parsing the cookie you posted was an additional space before 'mysite'. Using the console and logging the results of each step can help you find small issues like that.
Split first by ','
loop and split by '='
unescape value to the right of the '='
Then in your case, loop and split by '&'
This routine should work for you ([Fiddle][5]):
// Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g, '');
};
}
function parseCookie(cookie) {
var vals = cookie.split(',');
var nameValue = {}, cur, mysite;
for (var i = 0; i < vals.length; ++i) {
cur = vals[i].split('=');
nameValue[cur[0].trim()] = unescape(cur[1]);
}
mysite = nameValue.mysite.split('&');
nameValue.mysite = {};
for (i = 0; i < mysite.length; ++i) {
cur = mysite[i].split('=');
nameValue.mysite[cur[0].trim()] = cur[1];
}
return nameValue;
}
var cookie = "PHPSESSID=oleGInunYQIP7QSLpDUVR3, mysite=usr%3DChrome%26hash%3D594f803b380a41396ed63dca39503542";
console.log(JSON.stringify(parseCookie(cookie)));
Result:
{"PHPSESSID":"oleGInunYQIP7QSLpDUVR3","mysite":{"usr":"Chrome","hash":"594f803b380a41396ed63dca39503542"}}
You can retrieve any value you need by something like:
var usr = result.mysite.usr;

javascript - Get a portion of the cookie value with substr method

I have a cookie called "login" that contains a structure like "username|hashcode|salt".
Here's my code:
function readTheCookie(the_info)
{
var the_cookie = document.cookie;
var the_cookie = unescape(the_cookie);
var broken_cookie2 = the_cookie.substr(6);
alert(broken_cookie2);
}
readTheCookie('login');
I'ts giving me
pickup22|d47f45d141bf4ecc999ec4c083e28cf7|4ece9bce292e1
Now I just want the first part (everything before the first pipe , in that case, I want pickup22)
How can I do that? Cause the username will never be the same, so I cant put a "fixed" lenght.
Any help appreciated!
var readTheCookie = function (the_info) {
var the_cookie = document.cookie.split(";"), a = the_cookie.length, b;
for (b = 0; b < a; b += 1) {
if (the_cookie[b].substr(0, the_info.length) === the_info) {
return the_cookie.split("=")[1].split("|")[0];
}
}
if (b === a) {
return "";
}
},
username = readTheCookie('login');
That is nice and compact, plus easy to read, and finally it is JSLint compliant. Enjoy!
best way is to use split() method.
var parts = new Array();
parts = broken_cookie2.split("|");
var username = parts[0];
var hashcode = parts[1];
var salt = parts[2];

Transform a string into array using javascript

I have a string like this:
string = "locations[0][street]=street&locations[0][street_no]=
34&locations[1][street]=AnotherStreet&locations[1][street_no]=43";
What must I do with this string so i can play with locations[][] as I wish?
You could write a parser:
var myStr = "locations[0][street]=street&locations[0][street_no]=34&locations[1][street]=AnotherStreet&locations[1][street_no]=43";
function parseArray(str) {
var arr = new Array();
var tmp = myStr.split('&');
var lastIdx;
for (var i = 0; i < tmp.length; i++) {
var parts = tmp[i].split('=');
var m = parts[0].match(/\[[\w]+\]/g);
var idx = m[0].substring(1, m[0].length - 1);
var key = m[1].substring(1, m[1].length - 1);
if (lastIdx != idx) {
lastIdx = idx;
arr.push({});
}
arr[idx * 1][key] = parts[1];
}
return arr;
}
var myArr = parseArray(myStr);
As Shadow wizard said, using split and eval seems to be the solution.
You need to initialize locations first, if you want to avoid an error.
stringArray=string.split("&");
for (var i=0;i<stringArray.length;i++){
eval(stringArray[i]);
}
However, you might need to pay attention to what street and street_no are.
As is, it will produce an error because street is not defined.
Edit: and you'll need to fully initialize locations with as many item as you'll have to avoid an error.

Categories