JavaScript code won't work on some browsers
I created code that will work same as facebook add friends to send message list ( filed To: ). Code is created in FrontPage, also it's tested there, with limited functions, as FrontPage preview is, and it's working... Then it's tested in IE 7 and IE 8 and even IE 9, and it also works... Also tested in FireFox, Chrome and Safari, and it's working also...
Then, I send this code to friend, and he said it's not working... I'm looking over code all over again and again, and I can't find what can be wrong...
Code info:
topolje - div that contains list of users, and when you click on specific user it call function addnewto('username')
howbig - background div that counts size of new created user field, so input field can be resized to fit field where is list of users and input...
newone - input where you start typing username and it shows down usernames that start with that...
getRefToDiv(id_of_element) - find element by it's id in different browsers
Here is code:
var skup = new Array();
var skup2 = new Array();
function addnewto(user)
{
var i = 777777;
var userform = '';
var k = 0;
var velicina = 23;
getRefToDiv('topolje').style.height = "23px";
for ( j = 0; j < skup.length; j++ ) {
if ( skup[j] == "" ) {
i = j;
}
else
{
userform2 = '<font id="tekst' + j + '" name="tekst' + j + '" style="background: #8BBC40; border:1px solid #808080; -moz-border-radius: 3px; border-radius: 3px; padding-left:4px; padding-right:4px; padding-top:1px; padding-bottom:1px" face="Tahoma" size="2" color="#FFFFFF">' + skup2[j] + ' |<span onclick="';
userform2 += "this.parentNode.parentNode.removeChild(getRefToDiv('tekst" + j + "-1')); this.parentNode.parentNode.removeChild(this.parentNode); skup.splice(" + j + ",1); skup2.splice(" + j + ",1); setTimeout('getsize();', 10);;";
userform2 += '" style="cursor: pointer"> x </span></font><span id="tekst' + j + '-1" name="tekst' + j + '-1"> </span>';
var mast = getRefToDiv('howbig');
var duzina = 0;
mast.innerHTML = userform + userform2;
duzina = mast.clientWidth;
if ( duzina > 405 ) {
userform += "<br>" + userform2;
velicina += 21;
getRefToDiv('topolje').style.height = velicina + "px";
}
else
{
userform += userform2;
}
}
}
if ( i = 777777 ) {
i = skup.length;
}
skup[i] = 'tekst' + i;
skup2[i] = user;
userform2 = '<font id="tekst' + i + '" name="tekst' + i + '" style="background: #8BBC40; border:1px solid #808080; -moz-border-radius: 2px; border-radius: 2px; padding-left:4px; padding-right:4px; padding-top:1px; padding-bottom:1px" face="Tahoma" size="2" color="#FFFFFF">' + user + ' |<span onclick="';
userform2 += "this.parentNode.parentNode.removeChild(getRefToDiv('tekst" + i + "-1')); this.parentNode.parentNode.removeChild(this.parentNode); skup.splice(" + i + ",1); skup2.splice(" + i + ",1); setTimeout('getsize();', 10);";
userform2 += '" style="cursor: pointer"> x </span></font><span id="tekst' + i + '-1" name="tekst' + i + '-1"> </span>';
var mast = getRefToDiv('howbig');
var duzina = 0;
mast.innerHTML = userform + userform2;
duzina = mast.clientWidth;
if ( duzina > 405 ) {
userform += "<br>" + userform2;
velicina += 21;
getRefToDiv('topolje').style.height = velicina + "px";
}
else
{
userform += userform2;
}
getRefToDiv('topolje').innerHTML = userform + '<input type="text" onblur="if ( this.value.length < 3 ) { proveri(0); } else { proveri(2); }" onfocus="if ( this.value.length < 2 ) { proveri(1); }" onkeyup="if ( this.value.length > 2 ) { proveri(this.value); }" style="border: 1px solid #FFFFFF; width: 10px; font-family:Verdana; font-size:10pt; color:#8BBC40;" id="newone" name="newone">';
reorder();
}
function reorder()
{
var i = 777777;
var userform = '';
var userformx = '';
var k = 0;
var velicina = 23;
getRefToDiv('topolje').style.height = "23px";
for ( j = 0; j < skup.length; j++ ) {
if ( skup[j] == "" ) {
i = j;
}
else
{
userform2 = '<font id="tekst' + j + '" name="tekst' + j + '" style="background: #8BBC40; border:1px solid #808080; -moz-border-radius: 3px; border-radius: 3px; padding-left:4px; padding-right:4px; padding-top:1px; padding-bottom:1px" face="Tahoma" size="2" color="#FFFFFF">' + skup2[j] + ' |<span onclick="';
userform2 += "this.parentNode.parentNode.removeChild(getRefToDiv('tekst" + j + "-1')); this.parentNode.parentNode.removeChild(this.parentNode); skup.splice(" + j + ",1); skup2.splice(" + j + ",1); setTimeout('getsize();', 10);;";
userform2 += '" style="cursor: pointer"> x </span></font><span id="tekst' + j + '-1" name="tekst' + j + '-1"> </span>';
var mast = getRefToDiv('howbig');
var duzina = 0;
mast.innerHTML = userform + userform2;
duzina = mast.clientWidth;
if ( duzina > 405 ) {
userform += "<br>" + userform2;
userformx = userform2;
velicina += 21;
getRefToDiv('topolje').style.height = velicina + "px";
}
else
{
userform += userform2;
userformx += userform2;
}
}
}
getRefToDiv('topolje').innerHTML = userform + '<input type="text" onblur="if ( this.value.length < 3 ) { proveri(0); } else { proveri(2); }" onfocus="if ( this.value.length < 2 ) { proveri(1)开发者_如何学JAVA; }" onkeyup="if ( this.value.length > 2 ) { proveri(this.value); } else { proveri(1); }" style="border: 1px solid #FFFFFF; width: 10px; font-family:Verdana; font-size:10pt; color:#8BBC40;" id="newone" name="newone">';
var mast = getRefToDiv('howbig');
var duzina = 0;
mast.innerHTML = userformx;
duzina += mast.clientWidth;
var duzina2 = 400 - duzina;
getRefToDiv('newone').style.width = duzina2 + 'px';
}
I'm working on this for very long time, and I can't figure this out... If you find something that can be wrong, please tell me...
Thanks.
I don't even know how this works in any browser.
This:
if ( i = 777777 ) {
i = skup.length;
}
should be this:
if (i === 777777) {
i = skup.length;
}
This is a common bug in code referred to as "Accidental Assignment." That is, unless you ALWAYS want the conditional block to evaluate true, in which case your code is equivalent to:
i = 777777;
i = skup.length;
Which I'm sure you don't mean. I would recommend switching away from a WYSIWYG editor in favor of a good IDE that would point these things out to you (NetBeans, for example, highlighted this line for me as soon as I pasted your code into a new blank javascript file).
And, that is just one issue of many. See my comment on the question for instructions for solving all of the other problems.
The problem is in line 217 and line 42
精彩评论