473,804 Members | 2,296 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How Does JavaScript Call Forth CSS??


Okay, Folks,

I guess my real burning concern all along is a "high-level" one: just
how does JavaScript interact with CSS?

Right now, my newbie self only knows JavaScript and CSS to *co-
exist*...but I'm beginning to get the sense that they actually
interact -- or, perhaps more precisely, JavaScript acts upon CSS...but
how, exactly??

I see how JavaScript acts upon HTML, but I'm not so sure where
JavaScript acts upon CSS...moreover, I want to see where they could
create whole new browsing experiences, beyond simply new text and new
images (though I'm only just beginning at the level of new ways of
presenting text and images)....

How do I go about all this?? Ugh, I hope all those $$$ worth of books
will help...so far, I'm learning very basic things which I actually
wouldn't use (though the learning itself is fun), while the things I
would like to do, I either don't have the technical know-how for it or
I don't have the creative imagination to bring together what little I
do know -- sigh!!
Jun 27 '08
19 1745
"Harlan Messinger" <hm************ *******@comcast .netwrote in message
news:67******** *****@mid.indiv idual.net...
>Or do:

function activate_item() {
this.className= this.className. replace(/passive/,'active'); // change
passive to active
}

function passivate_item( ){
this.className= this.className. replace(/active/,'passive'); // change
active to passive
}

While easier to program, the downside to this as a general approach is
that you can wind up changing things you didn't mean to change.

class="one alone"

Replace class "one" with class "two":

class="two altwo"

which is probably not what was intended.
Yep but you can match regular expressions against whole words only which
would avoid that issue

Jun 27 '08 #11
On Apr 29, 3:36 pm, "Nik Coughlin" <nrkn....@gmail .comwrote:
"Harlan Messinger" <hmessinger.rem ovet...@comcast .netwrote in message

news:67******** *****@mid.indiv idual.net...
Or do:
function activate_item() {
this.className= this.className. replace(/passive/,'active'); // change
passive to active
}
function passivate_item( ){
this.className= this.className. replace(/active/,'passive'); // change
active to passive
}
While easier to program, the downside to this as a general approach is
that you can wind up changing things you didn't mean to change.
class="one alone"
Replace class "one" with class "two":
class="two altwo"
which is probably not what was intended.

Yep but you can match regular expressions against whole words only which
would avoid that issue
And that is something that was discussed on DHTML Central about 6
years ago. The first approach is the basic idea, but doesn't work, as
Harlan pointed out.

The pattern "(^|\\s)"+token +"($|\\s)" has been adopted by all the
libraries and further optimized to use non-capturing groups. The
pattern is now: new RegExp("(?:^|\\ s)"+token+"(?:$ |\\s)");

It can be a performance boost to cache the patterns (though only if
the cache is used). YUI seems to have discovered this, too.

Basic idea is that it's faster to get an object from a cache than
create one, even if it means an extra function call.
http://www.dhtmlkitchen.com/learn/js...exp-cache.html

http://www.dhtmlkitchen.com/ape/buil...classname-f.js

Garrett
Jun 27 '08 #12
On Apr 27, 2:20 pm, "david.karr " <davidmichaelk. ..@gmail.comwro te:
>

It's probably best to use reusable code for operations like this, and
fortunately all of the major JS frameworks include this. The
following URL provides a good summary of this:

http://www.openjs.com/scripts/dom/cl...nipulation.php

Cool, thanks! I'd actually managed to google up JQuery but I wasn't
sure about JavaScript "libraries" and all that....
Jun 27 '08 #13
On Apr 30, 6:59 pm, Prisoner at War <prisoner_at_.. .@yahoo.comwrot e:
On Apr 27, 2:20 pm, "david.karr " <davidmichaelk. ..@gmail.comwro te:
It's probably best to use reusable code for operations like this, and
fortunately all of the major JS frameworks include this. The
following URL provides a good summary of this:
http://www.openjs.com/scripts/dom/cl...nipulation.php
Wow, I guess libraries are still using capturing groups. I thought it
was pretty common knowledge to use non-capturing groups. They've got
that right in the tutorial and I can even see in Prototype JS:

removeClassName : function(elemen t, className) {
if (!(element = $(element))) return;
element.classNa me = element.classNa me.replace(
new RegExp("(^|\\s+ )" + className + "(\\s+|$)") , ' ').strip();
return element;
}

A capturing group stores the whitespace:
(\\s+|$)

A non-capturing group does not:
(?:\\s+|$)

It is less efficient to use a capturing group.
A capturing group increases the count of the backreference number, so
for more complicated expressions, it would be harder to keep track of
which group is which number, e.g. \1 or $1.

It would actually be possible to make the Prototype JS function even
less efficient by adding the batch() functionality of YUI. But I'd
have to day they've done a pretty bad for a major library. The
assignment in the conditional, the extra call to $(), the capturing
regexp (^|\\s+), the misnaming of trim() as "strip()" (and the adding
of this misnamed trim() function to String.prototyp e).

At first guess, it seems that Prototype JS's "strip()" is a
normalize() function, but it's not. It's a trim() function, misnamed.
trim() (or strip()) doesn't wipe out the whitespace in the middle, so
you end up with "foo bar baz quux" -remove "bar" -"foo baz quux" -
remove "baz -"foo quux". The add
It's quite surprising to see such code in such a popular library, at
least to me.

In takinga look at jQuery, i find another algorithm that is even
slower. So I have to take back what I said above about "the only thing
that could make the Prototype JS function slower". I didn't think
about the jQuery approach. Create a method "addClass" that delegates
to className.add, which splits the classname to an array, then uses an
each() function, then inside that function, check to make sure the
nodeType is 1.

Here it is:
http://code.jquery.com/jquery-latest.js

className: {
// internal only, use addClass("class ")
add: function( elem, classNames ) {
jQuery.each((cl assNames || "").split(/\s+/), function(i, className)
{
if ( elem.nodeType == 1 && !jQuery.classNa me.has( elem.className,
className ) )
elem.className += (elem.className ? " " : "") + className;
});
},
The function is intended to be called on a Decorated collection, which
would mean an extra call to each(). In most cases, that could be
replaced by adding a fragment identifier to the css file, and then
adding a class to the ancestor.

..window .link {}

/* adding a class to .window object's changes the links */
..window-raised .link { color: red }

/* jQuery tends to encourage this instead */
..window .link-active { color: red }

Cool, thanks! I'd actually managed to google up JQuery but I wasn't
sure about JavaScript "libraries" and all that....
I'm not so sure about those JavaScript libraries, either.
Jun 27 '08 #14
dhtml wrote:
On Apr 30, 6:59 pm, Prisoner at War <prisoner_at_.. .@yahoo.comwrot e:
>On Apr 27, 2:20 pm, "david.karr " <davidmichaelk. ..@gmail.comwro te:
>>It's probably best to use reusable code for operations like this, and
fortunately all of the major JS frameworks include this. The
following URL provides a good summary of this:
http://www.openjs.com/scripts/dom/cl...nipulation.php

Wow, I guess libraries are still using capturing groups. I thought it
was pretty common knowledge to use non-capturing groups. They've got
that right in the tutorial and I can even see in Prototype JS:
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
Therefore, chances are it is a really bad idea to do that. In fact, it
turns out that non-capturing parentheses are not supported before JavaScript
1.5, JScript 5.5, ECMAScript Ed. 3. Use them and your script will break in
IE before version 5.5, for example.
A capturing group stores the whitespace:
(\\s+|$)

A non-capturing group does not:
(?:\\s+|$)
True, after a fashion.
It is less efficient to use a capturing group.
Nonsense. The tiny amount of memory saved by not storing the backreference
is bought with the tiny amount of runtime that is required more to parse the
non-capturing expression.
F'up2 cljs

PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Jun 27 '08 #15
On May 1, 5:03 pm, dhtml <dhtmlkitc...@g mail.comwrote:
On Apr 30, 6:59 pm, Prisoner at War <prisoner_at_.. .@yahoo.comwrot e:
On Apr 27, 2:20 pm, "david.karr " <davidmichaelk. ..@gmail.comwro te:
It's probably best to use reusable code for operations like this, and
fortunately all of the major JS frameworks include this. The
following URL provides a good summary of this:
>http://www.openjs.com/scripts/dom/cl...nipulation.php

Wow, I guess libraries are still using capturing groups. I thought it
was pretty common knowledge to use non-capturing groups. They've got
that right in the tutorial and I can even see in Prototype JS:

removeClassName : function(elemen t, className) {
if (!(element = $(element))) return;
element.classNa me = element.classNa me.replace(
new RegExp("(^|\\s+ )" + className + "(\\s+|$)") , ' ').strip();
return element;
}

A capturing group stores the whitespace:
(\\s+|$)

A non-capturing group does not:
(?:\\s+|$)

It is less efficient to use a capturing group.
A capturing group increases the count of the backreference number, so
for more complicated expressions, it would be harder to keep track of
which group is which number, e.g. \1 or $1.

It would actually be possible to make the Prototype JS function even
less efficient by adding the batch() functionality of YUI. But I'd
have to day they've done a pretty bad for a major library. The
assignment in the conditional, the extra call to $(), the capturing
regexp (^|\\s+), the misnaming of trim() as "strip()" (and the adding
of this misnamed trim() function to String.prototyp e).

At first guess, it seems that Prototype JS's "strip()" is a
normalize() function, but it's not. It's a trim() function, misnamed.
trim() (or strip()) doesn't wipe out the whitespace in the middle, so
you end up with "foo bar baz quux" -remove "bar" -"foo baz quux" -
remove "baz -"foo quux". The add

It's quite surprising to see such code in such a popular library, at
least to me.

In takinga look at jQuery, i find another algorithm that is even
slower. So I have to take back what I said above about "the only thing
that could make the Prototype JS function slower". I didn't think
about the jQuery approach. Create a method "addClass" that delegates
to className.add, which splits the classname to an array, then uses an
each() function, then inside that function, check to make sure the
nodeType is 1.

Here it is:http://code.jquery.com/jquery-latest.js

className: {
// internal only, use addClass("class ")
add: function( elem, classNames ) {
jQuery.each((cl assNames || "").split(/\s+/), function(i, className)
{
if ( elem.nodeType == 1 && !jQuery.classNa me.has( elem.className,
className ) )
elem.className += (elem.className ? " " : "") + className;
});
},

The function is intended to be called on a Decorated collection, which
would mean an extra call to each(). In most cases, that could be
replaced by adding a fragment identifier to the css file, and then
adding a class to the ancestor.

.window .link {}

/* adding a class to .window object's changes the links */
.window-raised .link { color: red }

/* jQuery tends to encourage this instead */
.window .link-active { color: red }
Cool, thanks! I'd actually managed to google up JQuery but I wasn't
sure about JavaScript "libraries" and all that....

I'm not so sure about those JavaScript libraries, either.
Assignment in conditional is done to extend ('normalize') an element
(so that you can simply pass string into Element#removeC lassName) and
"return" if it's not a valid element. Such "normalizat ion" is
consistent throughout a library. Capturing groups, afaik, are not
supported by some of the browsers that Prototype supports. #strip is
named after Ruby's method - as you might know, prototype follows its
conventions in certain cases.

Best,
kangax
Jun 27 '08 #16
On May 1, 2:18 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
wrote:
dhtml wrote:
turns out that non-capturing parentheses are not supported before JavaScript
1.5, JScript 5.5, ECMAScript Ed. 3. Use them and your script will break in
IE before version 5.5, for example.
OK. It won't work in IE 5.0. A lot of stuff won't e.g. apply(),
push().
>
A capturing group stores the whitespace:
(\\s+|$)
A non-capturing group does not:
(?:\\s+|$)

True, after a fashion.
It is less efficient to use a capturing group.

Nonsense. The tiny amount of memory saved by not storing the backreference
is bought with the tiny amount of runtime that is required more to parse the
non-capturing expression.
No, it is somewhat faster to use a non-capturing group.

(function() {
var token = "bar",
exp = new RegExp("(^|\\s) "+token+"($|\\s )"),
t= new Date;
for(var i = 0; i < 10000; i++) {
exp.exec("foo bar baz quux");
}
return new Date - t;
})();

(function() {
var token = "bar",
exp = new RegExp("(?:^|\\ s)"+token+"(?:$ |\\s)"),
t= new Date;
for(var i = 0; i < 10000; i++) {
exp.exec("foo bar baz quux");
}
return new Date - t;
})();
[" bar ", " ", " "]
vs
[" bar "]

The first one is faster (and doesn't work in IE 5.0).

F'up2 cljs

PointedEarsv
Jun 27 '08 #17
On May 1, 6:34 pm, kangax <kan...@gmail.c omwrote:
On May 1, 5:03 pm, dhtml <dhtmlkitc...@g mail.comwrote:
On Apr 30, 6:59 pm, Prisoner at War <prisoner_at_.. .@yahoo.comwrot e:
On Apr 27, 2:20 pm, "david.karr " <davidmichaelk. ..@gmail.comwro te:
It's probably best to use reusable code for operations like this, and
fortunately all of the major JS frameworks include this. The
following URL provides a good summary of this:
http://www.openjs.com/scripts/dom/cl...nipulation.php
Wow, I guess libraries are still using capturing groups. I thought it
was pretty common knowledge to use non-capturing groups. They've got
that right in the tutorial and I can even see in Prototype JS:
removeClassName : function(elemen t, className) {
if (!(element = $(element))) return;
element.classNa me = element.classNa me.replace(
new RegExp("(^|\\s+ )" + className + "(\\s+|$)") , ' ').strip();
return element;
}
A capturing group stores the whitespace:
(\\s+|$)
A non-capturing group does not:
(?:\\s+|$)
It is less efficient to use a capturing group.
A capturing group increases the count of the backreference number, so
for more complicated expressions, it would be harder to keep track of
which group is which number, e.g. \1 or $1.
It would actually be possible to make the Prototype JS function even
less efficient by adding the batch() functionality of YUI. But I'd
have to day they've done a pretty bad for a major library. The
assignment in the conditional, the extra call to $(), the capturing
regexp (^|\\s+), the misnaming of trim() as "strip()" (and the adding
of this misnamed trim() function to String.prototyp e).
At first guess, it seems that Prototype JS's "strip()" is a
normalize() function, but it's not. It's a trim() function, misnamed.
trim() (or strip()) doesn't wipe out the whitespace in the middle, so
you end up with "foo bar baz quux" -remove "bar" -"foo baz quux" -
remove "baz -"foo quux". The add
It's quite surprising to see such code in such a popular library, at
least to me.
In takinga look at jQuery, i find another algorithm that is even
slower. So I have to take back what I said above about "the only thing
that could make the Prototype JS function slower". I didn't think
about the jQuery approach. Create a method "addClass" that delegates
to className.add, which splits the classname to an array, then uses an
each() function, then inside that function, check to make sure the
nodeType is 1.
Here it is:http://code.jquery.com/jquery-latest.js
className: {
// internal only, use addClass("class ")
add: function( elem, classNames ) {
jQuery.each((cl assNames || "").split(/\s+/), function(i, className)
{
if ( elem.nodeType == 1 && !jQuery.classNa me.has( elem.className,
className ) )
elem.className += (elem.className ? " " : "") + className;
});
},
The function is intended to be called on a Decorated collection, which
would mean an extra call to each(). In most cases, that could be
replaced by adding a fragment identifier to the css file, and then
adding a class to the ancestor.
.window .link {}
/* adding a class to .window object's changes the links */
.window-raised .link { color: red }
/* jQuery tends to encourage this instead */
.window .link-active { color: red }
Cool, thanks! I'd actually managed to google up JQuery but I wasn't
sure about JavaScript "libraries" and all that....
I'm not so sure about those JavaScript libraries, either.

Assignment in conditional is done to extend ('normalize') an element
(so that you can simply pass string into Element#removeC lassName) and
"return" if it's not a valid element. Such "normalizat ion" is
consistent throughout a library. Capturing groups, afaik, are not
supported by some of the browsers that Prototype supports. #strip is

Non-capturing groups that is.
Jun 27 '08 #18
@dhtml
remove "baz -"foo quux".
I believe your assertion is false, it still only leaves 1 space.
The pattern matches the beginning of the string OR one or more
<spacesthen the className then the end of the string OR one of more
<spacesafter.
This would remove <space>baz<spac eand replace with <spaceturning
the className value into "foo<space>quux ".

Info not directed at anyone:
Prototype doesn't officially support IE 5.5
http://www.w3schools.com/browsers/browsers_stats.asp
At some point ya have to let that version go :)

- JDD
Jun 27 '08 #19
On May 1, 4:05 pm, jdalton <John.David.Dal ...@gmail.comwr ote:
@dhtml
remove "baz -"foo quux".

I believe your assertion is false, it still only leaves 1 space.
The pattern matches the beginning of the string OR one or more
<spacesthen the className then the end of the string OR one of more
<spacesafter.
This would remove <space>baz<spac eand replace with <spaceturning
the className value into "foo<space>quux ".
Yes, you're right. THanks for correcting that.

It would then leave, in some cases, a space at the end, that gets
removed by strip()
Info not directed at anyone:
Prototype doesn't officially support IE 5.5http://www.w3schools.c om/browsers/browsers_stats. asp
At some point ya have to let that version go :)
IE 5.5 is difficult because of the different CSS box model. IE 5 is
even worse for lack of apply().

Putting this back to c.l.j, as that is where it seems most
appropriate.

Garrett
- JDD
Jun 27 '08 #20

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

Similar topics

7
1909
by: Fendi Baba | last post by:
The function is called from opencalendar(targetfield). Thanks for any hints on what could be the problem. .............................................................. var decimalPointDelimiter = ".";
4
2074
by: Guillaume CABANAC | last post by:
Hi everybody, Does anybody know how to access a remote database (say Oracle) from JavaScript code (within a Firefox Extension) ? I know ADO via ActiveX in the IE world and think a similar thing exists in Firefox. Thanks a lots,
3
12878
by: Angel | last post by:
Hi, I have a page which shows a matrix of names and grades for given period. All the grades fields must be editable and they can be up to 200 fields (20 students with 10 grades each) Since there are a lots of fields I do not want to go back and forth to validate entered values on any change. I decided that this may be achieved with some JavaScript functions to validate some controls and to calculate average and total values of other...
7
1414
by: endlesstide | last post by:
If I have two functions, and want to toggle back and forth between 1 and 2 from an onclick event.... how would I parse that? Thanks mucho <script> javascript function1() { something1....
1
3420
by: tshad | last post by:
I need to do some work with Javascript and my datagrid controls. I want to do something like: ********************************************************* function CheckQuestion() { var checkBox = document.getElementById('_ctl0_SecurityStandard'); if (checkBox.checked) { dropDown = document.getElementById('_ctl0_SecretQuestion');
1
2503
by: Eric Capps | last post by:
This may be more of a Java question, but I feel that JavaScript experts may be more qualified to help me find a solution. In short: is it possible to call a Java method from JavaScript, passing as an argument a JavaScript array (in my case, an array of doubles)? Supposing I have array x containing only doubles, I have tried: document.MyApplet.myMethod(x); But the method only ever seems to receive "null" as an argument.
4
4050
by: Java Guy | last post by:
I can find a lot of stuff on the internet about javascript, except how to I determine which version my IE6 is compatible with, or where to download javascript plugin/engine/what-ever for Windows (windows 98 se specifically). Basically, I don't write java or javascript code, but I want to update or tweak or fix how IE is handling javascript on my system currently. Where do I get that "stuff" ???
34
2539
by: -Lost | last post by:
I'm REALLY liking this so far. And for those who welcome something a little less cryptic than what the resident date guru offers, here's a chance to try something fairly elegant and definitely unique. http://www.datejs.com/ -- -Lost Remove the extra words to reply by e-mail. Don't e-mail me. I am kidding. No I am not.
16
1792
by: Prisoner at War | last post by:
Okay, Folks, I guess my real burning concern all along is a "high-level" one: just how does JavaScript interact with CSS? Right now, my newbie self only knows JavaScript and CSS to *co- exist*...but I'm beginning to get the sense that they actually interact -- or, perhaps more precisely, JavaScript acts upon CSS...but how, exactly??
0
9714
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9594
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10599
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10346
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10090
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7635
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5531
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.