javascript question

Events happening in the community are now at Drupal community events on www.drupal.org.
albercook's picture

Hello all,

I was asking Alex a question and he suggested that I post it to the group. I will include my question and Alex's response.

I'm try to make links on our webpage that, once clicked on, will open a window over the page to show additional content like a definition, picture or video clip. I was following this tutorial.

http://articles.sitepoint.com/article/create-pop-up-notes-dhtml

This all worked well and you can see it at

http://www.rocksandrobots.com/node/937

Until I read the last paragraph of the tutorial. Where it says that this
example only works for one object per page but "We will show you how to
do it with more than one object next month."

You guessed it! Next months
tutorial never came.

Alex's reply below


Just took a quick look. It looks like your javascript function showObject includes a parameter "id". You'll need to modify your "click here" link to pass an id to the showObject function. That function will then have to be changed to check which id was passed to it and then show the respective div (e.g. your current video div has id "n1"). You also may have to change your hideObject function also.


I have not had any time to look into how to do what Alex suggested. Below are some of the questions that I will be investigating.

1) Is this a reasonable way to proceed or am I missing some huge issues that I don't understand. I have a reasonable understanding of this code and it is my first javascript code so if there are no serious problems/issues I will likely continue in this direction.

2) Here is the function that changes the visibility of the element

// Show/Hide functions for pointer objects

function showObject(id) {
if (ns4) {
document.n1.visibility = "show";
}
else if (ie4) {
document.all['n1'].style.visibility = "visible";
}
else if (nn6) {
document.getElementById('n1').style.visibility = "visible";
}
}

//-->

I'm thinking that I need to understand how to change the syntax so that n1 is a variable. Will google for examples.

3) Since I'm in a node on drupal I don't really have access to the head of the document from within the edit window. The example script works but is with the and tags around the JavaScript function definitions. Is that cool or not?

Any thoughts that you have would be very helpful

Comments

Howdie. I personally suggest

xamox's picture

Howdie.

I personally suggest looking at using jQuery. It is part of drupal core and will make things a lot easier, plus it handles all cross-compatibility for you so you don't have to worry about code per browser.

The code is very simple using css ID's and classes as selectors. It has a bunch of nice plugins for visualization as well. For instance if that link you have has a CSS id of say #vid-link, then the jquery code would be as simple as:

$('#vid-link').click(
function()
{
$(this).get('html_code_here').slideDown();
}
);

I am also part of YAAARC which is in the mechshop and I know rocks and robots is located there. I won't be there tomorrow as I am out of town for work but would be happy to answer questions next time I am there.

Rather not learn jQuery right now

albercook's picture

We are currently running

Drupal 5. with jQuery 1.0.4

I understand that jQuery 1.0.4 is "not well supported." I know the we should upgrade but that is a job for another day.

Maybe that is not as efficient but for not I want to make this code work. I will be good for me to understand the javascript anyway.

Stubbornly,

George

Actually updating will only

xamox's picture

Actually updating will only take a few minutes.

See:
http://drupal.org/project/jquery_update

I assure you, it is worth the time to learn jQuery, there is a reason that John Resig (the creator of jquery) now works for mozilla on their javascript engine. I'm pretty sure you will be surprised on how true their slogan of "write less, do more" actually is true. Don't be frustrated, they have an excellent community supporting it as well, including drupal. You can probably find a lot of help on IRC at:

irc.freenode.net

In the channels:
"#drupal-support"
"#jquery"

Ann Arbor, Michigan

Group organizers

Group categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: