What's new

Little help with Javascript?

Status
Not open for further replies.

V1-Hyper

Well-known member
Joined
Feb 25, 2010
Messages
309
Reaction score
0
Hello all,

This is about my problem with the new Cryonite Grinding Calculator on The Hitchhiker's Guide to PG working in Opera and IE but not yet in FF, Chrome and Safari.
Neither Dreamweaver DS5 v11 nor Expression Web v3 prompt any browser incompatibility issues with this code, so I have no clue what’s itching here. But I read that different browsers sometimes use different JS commands for certain actions, but these details lie beyond me.
Maybe someone with JS skills can help? Thanks for any comments ;)
This is the xhtml code I use:
---Header Section---


<script type="text/javascript">
//Initialize var’s
var boost='0';
var sq='1';
var act='1';
function CryGrinder(boost,sq,act)
{
if (!document.getElementById) {return true;} //Browser handles getElementById?
var b=parseFloat(boost); // Cry Booster
var s=parseInt(sq); // Number of squad members
var a=parseInt(act); // of which actively grind
//check: selected active members never greater than selected squad members
if (a>s)
{
for (var i=1; i<=5; i++) {document.getElementById('act'+String(i)).removeAttribute('selected');}
document.getElementById('act'+sq).setAttribute('selected', "selected");
a=s;
}
// when sq changes, disable items of active members drop list greater than sq
for (var i=1; i<=s; i++) {document.getElementById('act' +String(i)).removeAttribute('disabled');}
for (var i= (s+1); i<=5; i++) {document.getElementById('act' +String(i)).setAttribute('disabled', "disabled");}
// calc cryonite and output
document.getElementById('output1').innerText=String(Math.round(b+ 100/s)+'%');
document.getElementById('output2').innerText=String(Math.round((b+100/s)*a)+'%');
}
</script>


---corresponding elements in Body Section---

<select id="Booster" onchange="return CryGrinder(Booster.value,sqMembers.value,activeMembers.value)">
<option value="0" selected="selected">no</option>
<option value="10">10%</option>
<option value="25">25%</option>
<option value="33.3">33%</option>
<option value="100">100%</option>
</select>
<select id="sqMembers" onchange="return CryGrinder(Booster.value,sqMembers.value,activeMembers.value)">
<option value="1" selected="selected">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<select id="activeMembers" onchange="return CryGrinder(Booster.value,sqMembers.value,activeMembers.value)">
<option id="act1" value="1" selected="selected">1</option>
<option id="act2" value="2" disabled="disabled">2</option>
<option id="act3" value="3" disabled="disabled">3</option>
<option id="act4" value="4" disabled="disabled">4</option>
<option id="act5" value="5" disabled="disabled">5</option>
</select>
<input type="text" id="output1" name="text1" readonly="readonly" />
<input type="text" id="output2" name="text2" readonly="readonly" />
 

Delorian

New member
Joined
Dec 22, 2010
Messages
7
Reaction score
0
Hi V1,

the elements that you are trying to populate are form fields and as such have a "value" and not innerText.

Therefore

Code:
document.getElementById('output1').innerText=String(Math.round(b+ 100/s)+'%');
document.getElementById('output2').innerText=String(Math.round((b+100/s)*a)+'%');
should be

Code:
document.getElementById('output1').value=String(Math.round(b+ 100/s)+'%');
document.getElementById('output2').value=String(Math.round((b+100/s)*a)+'%');
Cheers

Del
 

V1-Hyper

Well-known member
Joined
Feb 25, 2010
Messages
309
Reaction score
0
:sneaky: Umm..
Delorian, my HTML editor does not offer
.value as a valid property when I type in the code and reach the dot after document.getElementById('output1'). :sneaky:
Plz note, that my <input> tags are used as means for an output.

However, even changing
.innerText to .value is not yet the fix for the problem I'm afraid.
EDIT:
I now circumvented this issue alltogether by using
.setAttribute('value',String(Math.round(b+100/s)+'%'))

It's the upper header code that is not executed by FireFox and Chrome.
One can test that the drop-down entries will not be disable/enabled according to changes to the Squad member number, which is why those browsers' interpreters don't even get to the

document.getElementById('output1').innerText... lines.

The code as early as
document.getElementById('act'+... must somewhere contain an incompatibility... :(
 

peck

Well-known member
Joined
Jun 2, 2010
Messages
790
Reaction score
0
what editor format do you use? some allow you to establish browser specific code and most allow manual input.
 

V1-Hyper

Well-known member
Joined
Feb 25, 2010
Messages
309
Reaction score
0
I use Expression Web 3 (not sure what format it is running in) but I know I can choose to ignore the editor's element property suggestion. I'm just not always confident enough to do that ;)
But the .innerText issue was not the main problem, I'm afraid. The code fails earlier.

Cheers
 

Delorian

New member
Joined
Dec 22, 2010
Messages
7
Reaction score
0
Ah, well I am an old school coder and do everything by hand in a text editor. I changed the values and got it working in IE8, Firefox, Safari and Chrome before i posted it to you.

I cut the snippet you posted and placed it into a new document, removed the
"---corresponding elements in Body Section---" bit changed innerText to value and worked sweet as nut. Oh the joys of web development.

All the best

Del
 

V1-Hyper

Well-known member
Joined
Feb 25, 2010
Messages
309
Reaction score
0
Okay now I'm speechless :eek:
The changed code still works in IE8,
now works on my iPhone Safari!
now it works in Chrome!
and you say FireFox? Well it does not work on FireFox 3.6.13 (tested on 2 diff. PCs) :frown:

So I shall say...
the code works.
^^ :confused:
 

Delorian

New member
Joined
Dec 22, 2010
Messages
7
Reaction score
0
HI V1,

if you are still having issues and the output is only for display, then maybe you can replace the <input> tags with <span> tags and then just set the innerHTML of the span tags with the output. This should work in all browsers.

replace
Code:
<input type="text" id="output1" name="text1" readonly="readonly" />
<input type="text" id="output2" name="text2" readonly="readonly" />
with
Code:
<span id="output1"></span>
<span id="output2"></span>
and replace
Code:
document.getElementById('output1').innerText=String(Math.round(b+ 100/s)+'%');
document.getElementById('output2').innerText=String(Math.round((b+100/s)*a)+'%');
with
Code:
document.getElementById('output1').innerHTML=String(Math.round(b+ 100/s)+'%');
document.getElementById('output2').innerHTML=String(Math.round((b+100/s)*a)+'%');
See how you get on with that.

Del

PS my version of FF is also 3.6.13 :)
 

ridoi

Well-known member
Joined
May 16, 2010
Messages
2,668
Reaction score
1
V1-Hyper said:
Okay now I'm speechless :eek:
The changed code still works in IE8,
now works on my iPhone Safari!
now it works in Chrome!
and you say FireFox? Well it does not work on FireFox 3.6.13 (tested on 2 diff. PCs) :frown:

So I shall say...
the code works.
^^ :confused:

v1 if it helps, it works on my firefox, beta version 4.0 xD
 

V1-Hyper

Well-known member
Joined
Feb 25, 2010
Messages
309
Reaction score
0
ridoi said:
v1 if it helps, it works on my firefox, beta version 4.0 xD
YEEHAA!
Thx ridoi for this good news.. I don't needa stress over it then anymore :mrorange:

All right you FF users out there, plz update ur browser asap ;)
 

Manny

Well-known member
Joined
Feb 24, 2010
Messages
307
Reaction score
0
V1-Hyper said:
ridoi said:
v1 if it helps, it works on my firefox, beta version 4.0 xD
YEEHAA!
Thx ridoi for this good news.. I don't needa stress over it then anymore :mrorange:

All right you FF users out there, plz update ur browser asap ;)
that doesn't mean the code is correct, only that firefox programmers are gracious ^^
read here: http://forum.pirategalaxy.com/viewtopic.php?f=22&t=8875&start=20#p82355 ;)
 

pbhuh

Well-known member
Joined
Feb 24, 2010
Messages
3,918
Reaction score
87
V1-Hyper said:
ridoi said:
v1 if it helps, it works on my firefox, beta version 4.0 xD
YEEHAA!
Thx ridoi for this good news.. I don't needa stress over it then anymore :mrorange:

All right you FF users out there, plz update ur browser asap ;)
V1-Hyper, could you add a box where we can put our own value and that it then calculates how much that cryonite gets?
 

V1-Hyper

Well-known member
Joined
Feb 25, 2010
Messages
309
Reaction score
0
Manny said:
that doesn't mean the code is correct, only that firefox programmers are gracious ^^
read here: http://forum.pirategalaxy.com/viewtopic.php?f=22&t=8875&start=20#p82355 ;)
Super super super, your tips and advises. Many thx Manny!!! :mrorange:

pbhuh said:
V1-Hyper, could you add a box where we can put our own value and that it then calculates how much that cryonite gets?
Pbhuh, which custom value would you like to be able to plug in, the Cry Booster value?
What is it good for if there are only given Boosters available for purchase?
Thanks
V1
 

pbhuh

Well-known member
Joined
Feb 24, 2010
Messages
3,918
Reaction score
87
V1-Hyper said:
Manny said:
that doesn't mean the code is correct, only that firefox programmers are gracious ^^
read here: http://forum.pirategalaxy.com/viewtopic.php?f=22&t=8875&start=20#p82355 ;)
Super super super, your tips and advises. Many thx Manny!!! :mrorange:

pbhuh said:
V1-Hyper, could you add a box where we can put our own value and that it then calculates how much that cryonite gets?
Pbhuh, which custom value would you like to be able to plug in, the Cry Booster value?
What is it good for if there are only given Boosters available for purchase?
Thanks
V1
maybe give like if you get 100 cryonite that it is then calculated how much it is if its split with 5 squadman all with 33% booster.
 

V1-Hyper

Well-known member
Joined
Feb 25, 2010
Messages
309
Reaction score
0
It would really just be the matter of 1 trivial multiplication. The Cry value you pick times the percentage spit out by the calculator:
Your example of a full squad with 33% booster gives 53%. Hence, a 100C crystal becomes worth 53C.

PS: Always only our own Cry Booster matters, never the booster of your sq members. That's quite different to the BP booster where others will ruin ur booster if they haven't also got one but help shooting :LOL:
 
Status
Not open for further replies.
Top