views:

369

answers:

2

Hi Guys,

I am trying to use the prototype framework to hide a '< div >' based on a particular URL. I don't have access to server side - so I have no choice but to do this using prototype [restriction of platform using prototype].

Wondering if someone could tell me how to do this in prototype framework ?

i.e. I tried to do this but doesn't work

Event.observe(window, 'load', function() {

var url = document.location.href;

if (url.indexOf('registered') >= 0) {
$$('#side-menu-right').hide(); }

if (url.indexOf('login') >= 0) { $$('#side-menu-left').hide(); }

});

Love some help ?

P.S - Never used Prototype [jQuery man right here yo!]

A: 

I just made a test case on JS Bin which complains:

Exception thrown: $$("#hello").hide is not a function
Caused by line (23/21): $$('#hello').hide();

(using latest version of Prototype)

When using:

$('hello').style.display = "none";

it works correctly, see example.

EDIT: I adjusted the example on JS Bin to conditionally add a class name to the body before the involved element is reached. Using

.registered-user #hello { display: none; }

The element doesn't show up at all. It's not the most neat solution, as you have to throw some script in the middle of your document, but it works. If someone knows a better solution, please tell.

<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/prototype/1/prototype.js"&gt;&lt;/script&gt;
<meta charset=utf-8 />
<title>JS Bin</title>
<style>
  .registered-user #hello { display: none; }
</style>
</head>
<body>
  <script>
    if (document.location.href.indexOf('registered')>=0)
      $$('body')[0].addClassName('registered-user');
  </script>
  <p id="hello">Hello World</p>
</body>
</html>
Marcel Korpel
hey thanks for the response :) any ideas how to fix ? i am not even sure if this is correct in prototype
Thomas
@Thomas: Added correction.
Marcel Korpel
thanks! one thing - this works now )) but when the page loads - it appears then the javascript kicks in.any way to use something like "document(ready)" or even when its not ready ? [not sure of such a function in prototype]
Thomas
@Thomas: you mean the flickering (element is shown and immediately hidden)? You can only prevent that when you change some CSS rules *before* the mentioned element is drawn. I'll make an example.
Marcel Korpel
thanks so much :) yeah basically the elements appears - "then" it is hidden
Thomas
legend - thanks a lot for the great answer. really appreciate it!
Thomas
@Thomas: Code was wrong, added ">=0"
Marcel Korpel
noticed that :) - all round legend!
Thomas
A: 

Change

$$('#side-menu-right').hide();

to

$('side-menu-right').hide();

Hope this helps.

Reason
1) As you can see, JQuery uses CSS notation while Prototype is straight Javascript (so no #side-menu-right for div with an ID).

2) Struts and Prototype uses $ and not $$.

The Elite Gentleman
hey thanks - i just keep getting $$('side-menu-right').hide(); is not a function ? i really not sure ? [ps - using both # and without]
Thomas
ah my mistake :) was using $$ not $ - cheers. fixed that. ;)
Thomas
Glad I helped, obviously with no votes... ;)
The Elite Gentleman