Using variable in URL=

Having problems with DHTML Menu? There is usually somebody here who knows the answer.
Post Reply
mjstehn
Beginner
Beginner
Posts: 3
Joined: Mon Jul 21, 2003 8:32 pm

Using variable in URL=

Post by mjstehn »

I searched and did not find anything related to this. I would like to use a variable in the url= section of the menu. I am going to set a variable equal to the current date and I want a current weeks news to open that file according to the date. Is is possible to use a variable in the url=, if so are there any special quotes or anything that need to be used? Thanks,
User avatar
John
 Team
 Team
Posts: 5967
Joined: Sun May 19, 2002 8:23 pm
Location: Phoenix, AZ
Contact:

Post by John »

Yes, it is possible.

What language?
John
mjstehn
Beginner
Beginner
Posts: 3
Joined: Mon Jul 21, 2003 8:32 pm

Post by mjstehn »

Javascript. I just want to add some Javascript code to the existing menu file. I will get the current month and day and create a string with .pdf at the end. I just want to use that variable for my url.

Thanks,
User avatar
kevin3442
Milonic God
Milonic God
Posts: 2460
Joined: Sat Sep 07, 2002 12:09 am
Location: Lincoln, NE
Contact:

Post by kevin3442 »

The aI() function is used to create a menu item. The paramted passed to that funtion when you define an item is a string (like "text=item text;url=http://www.whatever.com;"). In most examples you see the parameter passed to aI() is a literal string (text inside of quotes). But you can also do string concatenation to produce the single parameter from any combination of literals, string variables, and functions that return strings.

So, at the top of your menu_data.js file, you could make a function that builds a four digit string representing the month and day (mmdd), incorporate that into a url, then return the entire url. Like so.

Code: Select all

function getNewsURL()
{
  var currentDate = new Date();
  var currentMonth = currentDate.getMonth() + 1
  if (currentMonth < 10) currentMonth = "0" + currentMonth;
  var currentDay = currentDate.getDate();
  if (currentDay < 10) currentDay = "0" + currentDay;
  var newsURL = "http://www.newstuff.com/news/" + currentMonth + currentDay + ".pdf";
  return newsURL;
}
You could then incorporate the returned url into the aI() parameter by calling that function part way through building the string for the aI() parameter, like so:

Code: Select all

aI("text=News of the Day;url=" + getNewsURL() + ";status=Read the news;");
For today, the menu item above would have a url pointing to:

Code: Select all

http://www.newstuff.com/news/0811.pdf
Hope that helps,

Kevin
mjstehn
Beginner
Beginner
Posts: 3
Joined: Mon Jul 21, 2003 8:32 pm

Post by mjstehn »

Exactly what I was trying to do. Works great. I appreciate your help. I had the function OK, I just did not know to use the " + function + " in the url section.

Thanks again,

Mike
Post Reply