Milonic provide full featured pull down web menus for some of the worlds largest companies
click here to see what it can do for you

Download Milonic DHTML Menu
Buy Milonic DHTML Menu

Back To Start Of Archive
Taken From The Forum: Archived Topics for the old Version 3.0 JavaScript Menu
Forum Topic: Click to view post
Last Updated: Thursday February 8 2018 - 06:02:30

JavaScript Arguments


Poster: blueEinstein
Dated: Wednesday July 30 2003 - 0:56:07 BST

In the following code snippet:

addmenu("MainMenu",...
"Sub Menu","show-menu=DisplaySubMenu",,,1])

addmenu("DisplaySubMenu",...
"Function 1", "javascript:fnFunc1Submit()",,,1,
"Function 2", "javascript:fnFunc2Submit()",,,1,
"Function 3", "javascript:fnFunc3Submit()",,,1])

It depicts a scenario of a typical 2 level submenus. I need to, however, pass the MENU NAME to fnFuncYSubmit(), something like:

"Function 3", "javascript:fnFunc3Submit(parent.menuName)",,,1])

Is that possible?[/list]


Poster: kevin3442
Dated: Wednesday July 30 2003 - 5:42:33 BST

I may not get the question entirely... but you did specifically state that you want to pass the name of the parent menu. If that's the case, it seems to me that since you define the menu structure, you already know the name of any submenu's parent menu. In your example, it would be

Code: Select all

"Function 3", "javascript:fnFunc3Submit('MainMenu')",,,1


As I said, I may be missing something in your question. Are you perhaps generating the menus dynamically, where the menu names are not necessarily known prior to runtime?

Kevin

Run-time menu names passed by JavaScript


Poster: blueEinstein
Dated: Wednesday July 30 2003 - 19:54:40 BST

Yes, the solution you have stated above will definetely work for static menus. But, I'm using dynamically generated menus. And names aren't known prior to runtime.

The reason why I'm doing it this way, is in my store, I have a "drop down" menu for each item -- which contains submenus. i.e.

Product List
- Apple (ID 123)
+-- Add to Cart
+-- View Description
+-- Buy Now
- Mango (ID 124)
+-- Add to Cart
+-- View Description
+-- Buy Now
- Strawberry (ID 125)
+-- Add to Cart
+-- View Description
+-- Buy Now

******************************************************
I'd like to do the following:

addmenu("123",....,"show-menu=MainMenu",,,1])
addmenu("124",....,"show-menu=MainMenu",,,1])
addmenu("125",....,"show-menu=MainMenu",,,1])

addmenu("MainMenu",....,
"Add to Cart","javascript:fnAdd(parent.id)",,,1,
"View Description","javascript:fnView(parent.id)",,,1,
"Buy Now","javascript:fnBuy(parent.id)",,,1])
******************************************************


******************************************************
I think above scenario is better, rather than creating the following:

addmenu("123",....,"show-menu=MainMenu-123",,,1])
addmenu("124",....,"show-menu=MainMenu-124",,,1])
addmenu("125",....,"show-menu=MainMenu-125",,,1])

addmenu("MainMenu-123",....,
"Add to Cart","javascript:fnAdd(123)",,,1,
"View Description","javascript:fnView(123)",,,1,
"Buy Now","javascript:fnBuy(123)",,,1])

addmenu("MainMenu-124",....,
"Add to Cart","javascript:fnAdd(124)",,,1,
"View Description","javascript:fnView(124)",,,1,
"Buy Now","javascript:fnBuy(124)",,,1])

addmenu("MainMenu-125",....,
"Add to Cart","javascript:fnAdd(125)",,,1,
"View Description","javascript:fnView(125)",,,1,
"Buy Now","javascript:fnBuy(125)",,,1])
******************************************************

Any advise? Thanks


Poster: kevin3442
Dated: Thursday July 31 2003 - 1:27:34 BST

Ah yes... dynamic. I'm probably being dense, but one thing I can't really tell for certain from your description is if you want the name of a parent menu, or the name (id) of the calling item in the parent menu. You did say parent menu, but from your description of what you'd like to do and your diagram, it seems that knowing the calling menu item in the parent menu would make more sense. I guess from your diagram it seems that you'd have one menu with three items, like:

Apple | Mango | Strawberry

which might look something like this in code:

Code: Select all

addmenu(menu=[
"products",
,...,
,"Apple","show-menu=123",,,1
,"Mango","show-menu=124",,,1
,"Strawberry","show-menu=125",,,1
])

with each item opening a submenu for Add to Cart, View Description, or Buy Now. In that case, the parent menu would be the same for each submenu (the "products" menu in the above example). But the item within the parent menu would be different. Is it actually the id of the calling menu item you're after? Or is it the case that Apple, Mango, and Strawberry are indeed separate menus (that would seem cumbersome to me)?

You wouldn't happen to have a working example of how you're currently doing it would you... a URL you can post? At any rate, I have some ideas that would probably work, but I want to be sure I understand the goal exactly, to decrease the chance that I might just be spouting nonsense.

Kevin


Poster: blueEinstein
Dated: Thursday July 31 2003 - 2:26:53 BST

Thanks for your reply.

Here are my answers to your question:
- I'm after the ID of the item.
- I would like to create a generic javascript function that would handle the "Add to Cart", "View Description", "Buy Now".

Code: Select all

  fnAddToCart(id), fnViewDescription(id), fnBuyNow(id)


Essentially, I need to make the following to work:

Code: Select all

fnAddToCart(id)
{
    alert('Add to cart ' + id);
}

fnViewDescription(id)
{
    alert('View Description ' + id);
}

fnBuyNow(id)
{
    alert('Buy ' + id);
}


Code: Select all

addmenu(menu=["123",...."Apple",     "show-menu=SubMenu",,,1])
addmenu(menu=["124",...."Mango",     "show-menu=SubMenu",,,1])
addmenu(menu=["125",...."Strawberry","show-menu=SubMenu",,,1])
...
addmenu(menu=["SubMenu",...
"Add to Cart",       "javascript:fnAddToCart(parent.id)",,,1,
"View Description",  "javascript:fnViewDescription(parent.id)",,,1,
"Buy Now",           "javascript:fnBuyNow(parent.id)",,,1])


I need to submit the ID (123, 124, 125) to the javascript function. This way, I do NOT have to create menus (SubMenu-123, SubMenu-124, SubMenu-125).

What should I replace parent.id with? Apologies if I couldn't make it any clearer.


Poster: kevin3442
Dated: Thursday July 31 2003 - 16:46:27 BST

OK... I got it. Thanks for re-explaining. How about this: I find it difficult in version 3 to work backwards to identify a calling menu or item (unless there's a way that escapes me). So, how about working forward and setting the item id as you go... set the item's ID value when the pointer is on that item, before the user gets to the Add, View, or Buy menu. In other words, use a global (or a member of a global object) to keep track of the calling item's id. Use the onfunction property in "product" menu items to call a function that sets the global id to the product id whenever you mouse over that menu item. Then, in what you call MainMenu, the global id value will be available to whatever function you call (fnAdd, fnView, fnBuy) without having to find or pass a value. The pertinent piece of information is set on mouse over rather than on click, so that the ID will be updated each time you mouse over a menu item that lists a product. Code would look something like this:

Code: Select all

// ID Tracking

var itemID;

function setItemID(id)
{
  itemID = id;
}

Code: Select all

// Action Functions:

function fnAddToCart()
{
  alert('Add to cart ' + itemID);
}

function fnViewDescription()
{
  alert('View Description ' + itemID);
}

function fnBuyNow()
{
  alert('Buy ' + itemID);
}

Code: Select all

// Typical product menu items:

,"Apple (ID=123)","show-menu=action","# onfunction=setItemID(123)",,1
,"Mango (ID=124)","show-menu=action","# onfunction=setItemID(124)",,1

Code: Select all

// Action submenu items:

,"Add to Cart","javascript:fnAddtoCart()",,,1
,"View Description","javascript:fnViewDescription()",,,1
,"Buy Now","javascript:fnBuyNow()",,,1


Should work. Having said all that, if you are just starting to build your site, have you considered using version 5 of the menu, now in release candidate 4?

Hope that helps,

Kevin


Poster: blueEinstein
Dated: Thursday July 31 2003 - 20:12:49 BST

Kevin, Thanks; it works perfectly.