Hello, I have designed a website that has some things for only registered users. I have javascript to hide these object if the user is not logged in using <body onload="a();">. But the problem is that when the page loads these items show up for a while and then hide I want to stop that.

Thanks

What you are probably doing is setting the default action to shown when the default action should be hidden. Then if a user is logged in the hidden items are then shown rather than the other way around.

Thanks for quick reply.

for example i want to hide a button from unregistered users, but it shows up fro 4-5 seconds and then hides.
i am using
document.getElementById('btn').style.visibility = 'none';

on body load

Thanks for quick reply.

for example i want to hide a button from unregistered users, but it shows up fro 4-5 seconds and then hides.
i am using
document.getElementById('btn').style.visibility = 'none';

on body load

Right, exactly, you are waiting until the page fully loads until you hide the button with your page load event. You should use

<input id="btn" type="button" style="display:none" />

Then your javascript onload event will check if the user is authenticated and display the button with:

document.getElementById('btn').style.display = 'block';

or

document.getElementById('btn').style.display = 'inline';

You've got the answer above, but remember that people can "view source" and see things hidden from the page display...just a thought to keep in mind.

Comments
good point

Any html element can be hidden/shown using this method.

Images are 'inline' elements
Table are 'block' elements

Ciao

Thanks a lot for the solution
same can be used for an image or table?

Yes you can, and as langsor stated, this is not for security, if you are trying to restrict access to something you should go with a server side solution. This will only hide it from plain site, but just remember that it is not hard to view all of your javascript no matter where you are pulling it from. Even if you don't see the javascript in the source and are getting it from another file, it can be read with things like the "web developer" add on with firefox and other applications.

I am using php but i haven't figured out how to hide elements based on security, can you suggest what to do?

If you are looking for a server side security mixed with the wonders of javascript then an ajax solution would be the way to go.

no you said that i should go with a server side solution, can i hide object with php, or any thing to ristrict users from getting to an object.

Thanks

no you said that i should go with a server side solution, can i hide object with php, or any thing to ristrict users from getting to an object.

Thanks

Unfortunately, there is a learning curve to it but I will say that you want to use session variables. I would start with doing a google search on password protection using sessions.

Hello thanx for your help:
The solution i have figured out is
<?php
if(isset($_SESSION)){?>
<td>Hello</td>
<?php } ?>

is it secure?

Hello thanx for your help:
The solution i have figured out is
<?php
if(isset($_SESSION)){?>
<td>Hello</td>
<?php } ?>

is it secure?

Yes, this is secure. I actually do this normally to not only verify that the session variable isset but that it isn't empty either:

<?php
if(isset($_SESSION['id']) && trim($_SESSION['id']) != ""){?>
<td>Hello</td>
<?php } ?>
This article has been dead for over six months. Start a new discussion instead.