473,383 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

A simple simple comparison question

db
Hi@all

Just got a comparison problem with javascript.

I want to compare :
document.getElementById(currentID).getAttribute("s tyle") with a string,
e.g: "background-color: lightgreen;" They are exactly the same, but
the execution result seems they are not equal. It works with firefox,
but not with IE.

My code is as following:
..............
var input = document.getElementById("someID");
var content = input.getAttribute("style");
if(content != "background-color: lightgreen;") <---this
comparison does not work with IE
input.style.background = "lightblue";
..............

I also tried
..............
var input = document.getElementById("someID");
if(input.style.background != "lightgreen")<---this comparison
does not work with IE either
input.style.background = "lightblue";
..............

Both of them work with Firefox. I really appreciate it if someone could
give me a hint.

cu

db

Sep 27 '06 #1
7 1643
db said the following on 9/27/2006 10:26 AM:
Hi@all

Just got a comparison problem with javascript.

I want to compare :
document.getElementById(currentID).getAttribute("s tyle") with a string,
e.g: "background-color: lightgreen;" They are exactly the same, but
the execution result seems they are not equal. It works with firefox,
but not with IE.

My code is as following:
..............
var input = document.getElementById("someID");
var content = input.getAttribute("style");
alert(content)

And see what it gives you to compare against.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 27 '06 #2
db

Randy Webb schrieb:

alert(content)

And see what it gives you to compare against.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Hi,

thanks for the reply. I have tried with alert thousand times......
with IE, it returns [object], with firefox it returns the expected
string

db

Sep 27 '06 #3
db wrote:
Hi@all

Just got a comparison problem with javascript.

I want to compare :
document.getElementById(currentID).getAttribute("s tyle") with a string,
e.g: "background-color: lightgreen;" They are exactly the same, but
the execution result seems they are not equal. It works with firefox,
but not with IE.

My code is as following:
..............
var input = document.getElementById("someID");
var content = input.getAttribute("style");
if(content != "background-color: lightgreen;") <---this
comparison does not work with IE
input.style.background = "lightblue";
..............

I also tried
..............
var input = document.getElementById("someID");
if(input.style.background != "lightgreen")<---this comparison
does not work with IE either
input.style.background = "lightblue";
..............

Both of them work with Firefox. I really appreciate it if someone could
give me a hint.

cu

db
CSS implementations may vary from browsers to browsers. All of these
implementations have their up side, and down side. The example below
illustrate such implementations of the style properties :

---------------------------------------------------
<html>
<head>
</head>
<body>

<p id="par1" style="background-color: lightblue;">
Hello world !
</p>

<script type="text/javascript">

var test1 = 'lightblue';

var test2 = document.getElementById("par1").getAttribute('styl e');

if ( ( typeof test2 == 'string' &&
test2 == 'background-color: ' + test1 + ';')
|| test2['backgroundColor'] == test1 ) {

alert( 'equal' );
} else {
alert( 'not equsl' );
}

</script>

</body>
</html>
---------------------------------------------

As you can see, when test2 is a string, you have a (for exemple)
Mozilla browser, whereas style is an object (not necessary to compare
that one if it is not a stsring) you have a IE browser (since
implementations may vary from browser to browser, such affirmation is
somewhat bold, do not consider this as a universal truth).

Moreover, if the style should be "background: lightblue;" (yes, it's
valid CSS), or "background-Color:lightblue;", then only IE-like CSS
implementation would return "equal" even when the color is right.

I would suggest that you set classes to your elements, and compare
class names instead of style properties. Something like this :

-----------------------------------------
<html>
<head>

<style type="text/css">
fontClass { font-weight: bold; }
colorClass { background-color: lightblue; }
</style>

</head>
<body>

<p id="par1" class="fontClass">
Hello world !
</p>

<script type="text/javascript">

var test1 = 'colorClass';

var test2 = document.getElementById("par1").className;

if ( test2.indexOf( test1 ) -1 ) {
alert( "equal" );
} else {
alert( "not equal" );
}

</script>

</body>
</html>
-----------------------------------------

This way, you won't have to worry about how the CSS is written ; no
regular expression or ugly string manipulations.

Hope this helps.

-Yanick

Sep 27 '06 #4
Yanick wrote:
[...]
<style type="text/css">
fontClass { font-weight: bold; }
colorClass { background-color: lightblue; }
</style>

should be :

<style type="text/css">
.fontClass { font-weight: bold; }
.colorClass { background-color: lightblue; }
</style>

Sep 27 '06 #5
VK

db wrote:
thanks for the reply. I have tried with alert thousand times......
with IE, it returns [object], with firefox it returns the expected
string
"style" attribute is not an attribute in the common HTML sense, like
say "id", "longdesc" etc. It is not a named attribute just holding a
string value you can simply assign and read back by
setAttribute/getAttribute.
It is a DOM interface, it means that on assigning new value to it you
are triggering CSS parsing engine, and the results of parsing may
differ dramatically from the initial argument (string value you tried
to assign). Taking for the simplicity only two browsers to compare with
(IE and Firefox):

<html>
<head>
<title>Style</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function demo() {
var foo = document.getElementById('foo');

// Both return DOM interface object
// on direct property use:
//
// window.alert(typeof foo.style);

// Now IE still returns DOM interface object
// while FF returns the attribute string value:
//
// window.alert(typeof foo.getAttribute('style'));

// but even in FF it is not the same string:
// it was parsed on load and by parsing rules
// the first illegal property and enything after
// was ignored and doesn't exists anymore
// (no more foobar "CSS rule"):
//
//window.alert(foo.getAttribute('style'));

window.alert(foo.style.backgroundColor);

}
window.onload = demo;
</script>
</head>

<body>
<div id="foo"
style="width:200px; background-color: lightgreen; foobar:
rulez"></div>
</body>
</html>

you chould never use style attribute/property for any comparison
operations. Use direct style.someRule comparison - but with a big
caution as you can get a normalized value.

Better yet use classes as already suggested.

Sep 27 '06 #6
db
Thanks for your great help!

CSS field is still a blank area for me, without your hints, i have no
chance to get through, really appreciate it!

db

Sep 28 '06 #7
JRS: In article <11********************@h48g2000cwc.googlegroups.c om>,
dated Wed, 27 Sep 2006 07:26:31 remote, seen in
news:comp.lang.javascript, db <ad******@gmail.composted :
>
I want to compare :
document.getElementById(currentID).getAttribute(" style") with a string,
e.g: "background-color: lightgreen;" They are exactly the same, but
the execution result seems they are not equal. It works with firefox,
but not with IE.
Try, in various browsers,
F = document.getElementById(currentID)
Z = F.getAttribute("style")
for (J in Z) document.write(Z[J], "<br>")

It's a good idea to read the newsgroup and its FAQ. See below.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 28 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Frag | last post by:
Hi guys, I searched around without any clear answer. Tons of stuff, but nothing concrete. I am trying to call this Javascript function: function ButtonClicked() { alert("The button has...
11
by: greg.scharlemann | last post by:
I've been playing with this form validation method for a while and have tried an array of things but haven't had any luck with a couple items. 1. The validateForm() function doesn't detect when...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
7
by: Fernando Cacciola | last post by:
Hi, I'm terribly new at C# (come from C++ land). I'm making some benchmarks to see the effect of different coding styles, and I run across a situation that strikes me as pretty odd. For my image...
2
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when...
27
by: karan.shashi | last post by:
Hey all, I was asked this question in an interview recently: Suppose you have the method signature bool MyPairSum(int array, int sum) the array has all unique values (no repeats), your...
2
by: eastern_strider | last post by:
I'm running into problems about defining a comparison function for a map which has a user defined key. For example: class Key { public: string name; int number; Key (na, nu) : name (na),...
3
by: =?Utf-8?B?R0I=?= | last post by:
I have created a small program that illustrates the problem. I would know how to address the fields that I want to sort on in the greaterThan comparison. Anybody who knows?? using System; ...
5
by: evanevankan2 | last post by:
I have a question about the warning 'comparison between signed and unsigned' I get from my code. It comes from the conditional in the outer for loop. I understand the warning, but I'm not sure...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.