/    Sign up×
Community /Pin to ProfileBookmark

string compare problem

I have

var x = new Array()
x[0] = ‘text 0’
x[1] = ‘text 1’

and somewere in code an if statement:

if(document.getElementById(‘anid’).innerHTML == x[1])

Everythig works OK. Now, if I use another type of string

var x = new Array()
x[0] = ‘<img src=”0.jpg”>’
x[1] = ‘<img src=”1.jpg”>’

The if satement woun’t work (I mean it works as if those two strings, innerHTML and the x value are [b]never[/b] equal). Why? Aren’t the second case values strings also?

Any ideea?

Here’s the whole script. It rotates proportional / random some objects. If simple text, is ok, but if pictures the first [b]break[/b] is always bypassed, thus the picture is loaded again, even if it is the same.

[code=php]
<html>
<head>
<script>
var c = new Array(50,50,90) // Add as many values, but the same number with var inn
var inn = new Array() // Add as many values, but the same number with var c
inn[0] = ‘<img src=”25.jpg”>’;
inn[1] = ‘<img src=”26.jpg”>’;
inn[2] = ‘<img src=”27.jpg”>’;
var timeout = 2000; // set de delay (miliseconds)

// No need any changes below
var tot = 0;
var aa = 0;
for (i=0;i<c.length;i++){
tot = tot+c[i];
}
cc = new Array()
for (i=0;i<c.length;i++){
cc[i] = c[i]/tot*100;
}
qq = new Array()
for (i=0;i<c.length;i++){
qq[i] = aa+cc[i];
aa=qq[i]
}

function slide(){
var rr=Math.floor(Math.random()*101);
for (i=0;i<c.length;i++){
if(rr<=qq[i]){
if(document.getElementById(‘x’).innerHTML == inn[i]){alert(‘1’); break}

else{document.getElementById(‘x’).innerHTML = inn[i];alert(‘2’);
break}
}
}
setTimeout(‘slide()’,timeout)
}
</script>
</head>

<body onload=”slide()”>
<div id=”x”></div><br>
</body>
</html>
[/code]

Use first
inn[0] = ‘text 00’;
inn[1] = ‘text 01’;
inn[2] = ‘text 02’;
When simple text, the alert(‘1’) shows whenever the random number keeps the same object. So the code is OK. When pictures instead of simple text, the first break woun’t work (the first if statement is somehow incorrect). Any ideeas?

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@FangJan 23.2004 — I guess you are using IE:

IE returns with the full path of the image, in Moz. it works fine.

Put an alert(document.getElementById('x').innerHTML) after the if statement to see what I mean.

IE and Moz. often return something different with innerHTML.

You may need to search for the substring image name ("25.jpg" etc.) in the innerHTML.
Copy linkTweet thisAlerts:
@KorauthorJan 24.2004 — I guess than that IE makes a sort of auto-edit, so I can not compare the two whole strings...

That means I have to find another way to see if the new loop picture is the same or not with the previous.

Is there a way to store a parameter out of the loop and call it back to compare with a value nest time? I think it is...
Copy linkTweet thisAlerts:
@KorauthorJan 24.2004 — OK, thanx all. Now that I found out why that happended, I was able to solve it by change the if statement's terms. I use a param to compare with the increment:

[code=php]
<html>
<head>
<script>
var c = new Array(50,50,90) // Add as many values, but the same number with var inn
var inn = new Array() // Add as many values, but the same number with var c
inn[0] = '<img src="25.jpg">';
inn[1] = '<img src="26.jpg">';
inn[2] = '<img src="27.jpg">';
var timeout = 2000; // set the delay (miliseconds)

// No need any changes below
var tot = 0;
var aa = 0;
for (i=0;i<c.length;i++){
tot = tot+c[i];
}
cc = new Array()
for (i=0;i<c.length;i++){
cc[i] = c[i]/tot*100;
}
qq = new Array()
for (i=0;i<c.length;i++){
qq[i] = aa+cc[i];
aa=qq[i]
}
var pp = -1;
function slide(){
var rr=Math.floor(Math.random()*101);
for (i=0;i<c.length;i++){
if(rr<=qq[i]){
if(pp==i){
break}
else{document.getElementById('x').innerHTML = inn[i];
pp=i;
break}
}
}
setTimeout('slide()',timeout)
}
</script>
</head>

<body onload="slide()">
<div id="x"></div><br>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@PittimannJan 24.2004 — Hi Kor!

Fang's suggestion would do it for you.

The problem is, that IE "writes" the full image path into the src of the image (if you test locally like "file:///driveLetter:/etc/filename"; if it is on the server like "http://blah..."

Instead of using Fang's proposal, you could just enter the full paths into the inn array. Please note: IE will write IMG with upper case, whereas src will always have to be lower case...

Mozilla uses lower case also for img.

So you could do that:
[code=php]
function slide(){
var rr=Math.floor(Math.random()*101);
for (i=0;i<c.length;i++){
if(rr<=qq[i]){
if(document.getElementById('x').innerHTML.toLowerCase() == inn[i].toLowerCase()){alert(1); break}

else{document.getElementById('x').innerHTML = inn[i];alert(2);
break}
}
}
setTimeout('slide()',timeout)
}
[/code]

Cheers - Pit

Edit: didn't see your latest post. - Your way of course is better ?
Copy linkTweet thisAlerts:
@KorauthorJan 24.2004 — Thanks Pittiman, I also saw the Uppercase and thought of an toLowerCase solution but I gave up, as I could not verify it locally, on my computer . Locally the IE auto-edit brings me something like D:///root/folder/subfolder/picture.jpg... I could use the split("/"), also, to verify the last array term but, unfortunately I have to use mixture of jpg and Flash banners...

The param solution came naturally after that. Thanx anyway.

In fact it is not my problem, one of our friends here brought it up, but it looked enough interesting to me to try hardly to solve.

Furthermore, I learned a lot now about how to compare a string to a tag in innerHTML which will cost me less pain later when I would have to solve a similar problem
Copy linkTweet thisAlerts:
@FangJan 24.2004 — It's always worth checking what each browser returns with innerHTML.

Even adding elements with the DOM produces different results.
Copy linkTweet thisAlerts:
@KorauthorJan 24.2004 — Yes, Fang, thanks for your help. I used to think that innerHTML should be common, I mean the content of the TAG and nothing elese. I see now that is not quite so...

I strongly feel the need of a unique and ferm attribute of an object which will return the exact content between <tag></tag>, and it seems like this is a big problem these days.
×

Success!

Help @Kor spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...