calling an id before he was created

N

neoswf

Hi Guys
i need to call an ID before he was created cause i want to change his
display status (none:block) and i dont want to load him and only after
he was loaded to change his display settings.

i tried an unValid techniuqe- to write on the fly inside the body,
couse only there i can do so, a style tag that chage the display
definitions, but i cannot place a style tag inside the body.

any ideas?
 
R

Randy Webb

neoswf said the following on 1/11/2006 5:12 AM:
Hi Guys
i need to call an ID before he was created cause i want to change his
display status (none:block) and i dont want to load him and only after
he was loaded to change his display settings.

<style type="text/css">

#IDThatDoesntExistYet{
display: none;
}
</style>
 
S

shlomi horovitz

but this ID isnt exist yet
ill try to explain again- i use JS, and by JS i ask if and id exist. i
cannot ask if he exist if he havent been created yet in the HTML.

now u see my point?
 
N

neoswf

can you please explain your self and not just throw words to the air?
i need an assistence, can you support it? saying dont do this wont
solve my problem
 
V

VK

neoswf said:
Hi Guys
i need to call an ID before he was created cause i want to change his
display status (none:block) and i dont want to load him and only after
he was loaded to change his display settings.

i tried an unValid techniuqe- to write on the fly inside the body,
couse only there i can do so, a style tag that chage the display
definitions, but i cannot place a style tag inside the body.

any ideas?

You cannot do that - at least you cannot do that with any guarantees
that it will always work. Addressing an element by id (or by name for
this matter) is only possible when the DOM structure is ready. And the
DOM structure is not ready until "load" event for window is fired. Same
with desktop applications: you cannot open say a menu until the damn
thing is finished to open (who ever worked on slow machines knows what
I'm talking about).

So no again: you cannot handle something that doesn't exists yet.
Possible workarounds depend heavily on what conditions are you
showing/hiding page elements.
And remember that you cannot as well take any decisions in relation
with other elements (like "if element A contains this, then show
element B") You can check only basic conditions before "load": a
feature test, browser userAgent string, cookie presence, screen size
etc.

So what is your objective?
 
R

RobG

shlomi said:
but this ID isnt exist yet

Using CSS it doesn't matter. The CSS is parsed first (it must be in the
head, div's aren't allowed in the head).

When (if) the HTML parser finds the element, it will set its display
property to none because it's already been told to do that by the CSS.

ill try to explain again- i use JS, and by JS i ask if and id exist. i
cannot ask if he exist if he havent been created yet in the HTML.

Exactly. If it doesn't exist, you can't get a reference to it.

now u see my point?

We all got it the first time. If you want to use script to modify an
element, the soonest that the script can act is immediately *after* the
element has been created. Nothing you write can change that - 'the moving
parser having parsed moves on...' - apologies to Omar Khayyam and Edward
FitzGerald.

To do stuff to the DIV as soon as possible, put the script immediately
after the closing tag of the DIV in the HTML source:


<div id="hideMe">
<!-- stuff -->
</div>
<script type="text/javascript">
if (document.getElementById){
var hideMe = document.getElementById('hideMe');
}
hideMe && hideMe.style && hideMe.style.display = 'none';
</script>
 
R

RobG

RobG wrote:
[...]
<div id="hideMe">
<!-- stuff -->
</div>
<script type="text/javascript">
if (document.getElementById){
var hideMe = document.getElementById('hideMe');
}
hideMe && hideMe.style && hideMe.style.display = 'none';

Sorry, getting late...

hideMe && hideMe.style && (hideMe.style.display = 'none');


The brackets are required to stop some browsers error-correcting the
assignment to an evaluation - i.e. changing '=' to '=='
 
T

Thomas 'PointedEars' Lahn

neoswf said:
can you please explain your self and not just throw words to the air?

Follow the link -- Google Groups makes the URI one -- and read
what I posted before.
i need an assistence, can you support it? saying dont do this wont
solve my problem

You either do not have a problem at all but you create one with your
nonsensical approach, or you have more problems than you described.


PointedEars
 
N

neoswf

first of all- thomas- i havent understood that the link u paste in your
messege is an article. it looked to me as kind of an Email, due to the
@PointedEars.de at the end of the link. please exept my appologies.

secondly, ill describe in details my problem: i have a site that in
some pages, i want to disapear the right OR left pannels.
so i display:none them using CSS.

so the solution i came up with is write a var and then write a function
that checks if the var = left OR right and the, using document.write,
ill write onThFly my CSS.

tommorow ill paste here the HTML page i created. its a template im very
proud of. its includes inside an html stracture that is very simple but
very powerfull.
 
R

Randy Webb

neoswf said the following on 1/11/2006 4:01 PM:

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.

first of all- thomas- i havent understood that the link u paste in your
messege is an article. it looked to me as kind of an Email, due to the
@PointedEars.de at the end of the link. please exept my appologies.

Ignore Thomas. About half of what he says makes sense, the other half is
rubbish. The great challenge of it all is to be able to distinguish the
two. If you get to that point, nothing he says becomes relevant to you.
secondly, ill describe in details my problem: i have a site that in
some pages, i want to disapear the right OR left pannels.
so i display:none them using CSS.

Then do it on the server. Simple problem, simple solution.
so the solution i came up with is write a var and then write a function
that checks if the var = left OR right and the, using document.write,
ill write onThFly my CSS.

Then do that.
tommorow ill paste here the HTML page i created. its a template im very
proud of. its includes inside an html stracture that is very simple but
very powerfull.

Does it include a spell checker?
 
N

neoswf

Randy said:
If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.

yes, i've recived replies from another webSite, DevSomething... it was
so strange, it looked to me like they sit on google.groups DB and still
all there threads...

About Tommas- now my misunderstanding him seems more logic. now i feel
sain... :)
Then do it on the server. Simple problem, simple solution.

i cannot do that. our CMS must remain simple and CANNOT be modified...
:( lucky me. this was the ultimate solution, but it cannot be done...
Does it include a spell checker?

and BMW also :)

good nighty night
NeoSwf
 
R

Randy Webb

neoswf said the following on 1/11/2006 4:43 PM:
Randy Webb wrote:




yes, i've recived replies from another webSite, DevSomething...

Yeah, they think they are a Usenet provider and they are almost as bad
as Google Groups. They send you emails to tell you someone replied to a
post of yours.
it was so strange, it looked to me like they sit on google.groups DB and
still all there threads...

No, Google sits on Usenet archives and claim them as Google Threads. You
are not posting to Google, you are posting to Usenet using a Google
interface.
About Tommas- now my misunderstanding him seems more logic. now i feel
sain... :)

<script type="text/javascript">
window.onload = hideIt;

function hideIt(){
//code here to hide the div tag.
}
 
N

neoswf

Randy said:
<script type="text/javascript">
window.onload = hideIt;

function hideIt(){
//code here to hide the div tag.
}

the reason i dont use onLoad is that i want to avoid dancing interface
of my site.
my site is a trade market website, that hold inside of him many
httpRequests tables with a lot of data. So if on table will stuck, till
it fully loads, tha layer will be shown, and only then, it will
disapear.
i want to avoid that, and to write at the head the display style of
each panel in my site.
 
R

RobG

neoswf said:
the reason i dont use onLoad is that i want to avoid dancing interface
of my site.
my site is a trade market website, that hold inside of him many
httpRequests tables with a lot of data. So if on table will stuck, till
it fully loads, tha layer will be shown, and only then, it will
disapear.
i want to avoid that, and to write at the head the display style of
each panel in my site.

Use document.write to write the appropriate CSS:

<head>
<title>...</title>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">

<style type="text/css">
#leftDiv { border: 1px solid red; float: left; width: 50%;}
#rightDiv { border: 1px solid green; float: right; width: 50%;}
</style>

<script type="text/javascript">

// Set this to left or right, whichever should be hidden
var hideLR = 'leftDiv';
var styleString = '<style type="text/css">'
+ '#' + hideLR + '{display: none;}'
+ '<\/style>';
document.write(styleString);

</script>
</head>

<body>
<div id="leftDiv">left div</div>
<div id="rightDiv">right div</div>
</body>


But it seems to me that you must write the value of 'hideLR' at the
server, and in that case you may as well write the CSS there too.
 
T

Tony

neoswf said:
the reason i dont use onLoad is that i want to avoid dancing interface
of my site.
my site is a trade market website, that hold inside of him many
httpRequests tables with a lot of data. So if on table will stuck, till
it fully loads, tha layer will be shown, and only then, it will
disapear.
i want to avoid that, and to write at the head the display style of
each panel in my site.

It is not possible to hide a DIV that hasn't been defined yet in
javascript. Doing this with a CSS style declaration in the page head
makes the most sense. However, if you absolutely must use javascript,
try this:

<div id="theDivToHide">
<!-- content here -->
</div>
<script type="text/javascript">
document.getElementById("theDivToHide").display = "none";
</script>


This is done after the div is rendered, but before the full page is
rendered. It should hide the div before it's ever seen.

Of course, anyone with javascript turned off will still see the div.
That wouldn't happen if you used CSS.
 
T

Thomas 'PointedEars' Lahn

RobG said:
Use document.write to write the appropriate CSS:

<head>
<title>...</title>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">

<style type="text/css">
#leftDiv { border: 1px solid red; float: left; width: 50%;}
#rightDiv { border: 1px solid green; float: right; width: 50%;}
</style>

<script type="text/javascript">

// Set this to left or right, whichever should be hidden
var hideLR = 'leftDiv';
var styleString = '<style type="text/css">'
+ '#' + hideLR + '{display: none;}'
+ '<\/style>';
document.write(styleString);

</script>
</head>

<body>
<div id="leftDiv">left div</div>
<div id="rightDiv">right div</div>
</body>

This is not going to work as the OP wants. But then it is even
nonsense to attempt such, especially on a "trade market website".
But it seems to me that you must write the value of 'hideLR' at the
server,

Nonsense.


PointedEars
 
R

RobG

Thomas said:
RobG wrote:




This is not going to work as the OP wants. But then it is even
nonsense to attempt such, especially on a "trade market website".

The OP has been offered two options, this is one. It 'works' exactly as
requested as far as I can see - it will almost certainly ensure that the
div is marked to be hidden before its HTML is parsed. It also means
that if scripting is disabled, both divs will be shown.

Whether it suits or not is a matter of opinion best left to the OP. Do
you intend to enlighten us as to why you consider that it won't work 'as
the OP wants'?

Nonsense.

I'll see your nonsense and raise you an OT.

Once upon a time you used to offer solutions when coaxed from your ivory
tower. Maybe I should go back to a munged e-mail address so you restore
me to your killfile[1]. ;-)

"it seems to me" is another way of writing "in my opinion", or, if you
prefer, IMHO. That opinion is based on my understanding of the OP's
requirements and the response to what has been offered so far. It does
not mean there is no other way or even this is *the* best solution.

There are a couple of ways that the logic for showing/hiding the divs
can be independent of the server - it could use a cookie stored on the
PC, or it could be based on a search string or hash value in the URL
passed from a previous page or modified location object.

But there are serious drawbacks to those approaches that make them not
worth offering in the current discussion, particularly as the OP hasn't
made mention of a requirement that might indicate their use.

It also makes no sense (to me) to send HTML to the client with the
intent to not display a large chunk of it.

If you disagree, explain why. If you wish to be helpful, provide an
alternative and explain why you think it will suit better. Simplistic
contradiction is just gainsaying and a complete waste of bandwidth.


1. I use a number of different PCs to access the web and news groups,
some have munged addresses and some don't. I am surprised that the
level of spam I get has not increased as a result of exposing a real
address, so I am gradually fixing the munged ones. Incidentally, it
hasn't stopped people from de-munging it and e-mailing me.
 
R

Randy Webb

Tony said the following on 1/11/2006 6:20 PM:

Of course, anyone with javascript turned off will still see the div.
That wouldn't happen if you used CSS.

Unless they had CSS Disabled. And to be honest, it is easier to find the
CSS Disable in IE6 than it is the JS Disable.

Tools>Internet Options>Accessibility>Custom Style Sheet.
 
T

Thomas 'PointedEars' Lahn

RobG said:
The OP has been offered two options,

No, there have been more, including mine that it is far better just
to let it be. Which applies especially to that field of application:
Customers that are provided _nothing_ (from their point of view) will
leave almost immediately, before the content _perhaps_ would be
displayed later.
this is one. It 'works' exactly as requested as far as I can see - it will
almost certainly ensure that the div is marked to be hidden before its
HTML is parsed. It also means that if scripting is disabled, both divs
will be shown.

And now try to show that `div' element again or just try to make the
document usable without client-side script _and_ CSS-DOM support --
surprise!

Know your CSS, but also know your CSS-DOM: using the `style' property of an
element object -- which would be necessary as Opera provides no means of
accessing style rules -- is equivalent to using the `style' attribute of
the element it represents. Although CSS defines that the specificity of
declarations in the `style' attribute of an element is the same as a style
rule with an ID selector, --

| In HTML, values of an element's "style" attribute are style sheet rules.
| These rules have no selectors, but for the purpose of step 3 of the
| cascade algorithm, they are considered to have an ID selector
| (specificity: a=1, b=0, c=0).

and

| 3. The secondary sort is by specificity of selector: more specific
| selectors will override more general ones. Pseudo-elements and
| pseudo-classes are counted as normal elements and classes,
| respectively.

-- not all UAs also honor cascading step 4 that says

| 4. Finally, sort by order specified: if two rules have the same weight,
| origin and specificity, the latter specified wins. Rules in imported
| style sheets are considered to be before any rules in the style sheet
| itself.

IIRC we had cases here proving that especially IE is broken in that regard,
and I know from personal experience that with IE often the first match wins,
not the last match. OK, you can access stylesheet rules with IE's CSS-DOM
and so could change the ID rule. But, come on, all this effort for a
really misguided and harmful "solution", efforts that will inevitably only
make a Bad Thing worse?


HTH

PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top