Question regarding Javascript functions and the url string

Having problems with DHTML Menu? There is usually somebody here who knows the answer.
Post Reply
Morat
Beginner
Beginner
Posts: 2
Joined: Thu Aug 28, 2003 6:33 pm

Question regarding Javascript functions and the url string

Post by Morat »

I need to, if at all possible, dynamically create url addresses. (It makes maintance of the site so much easier if I only have to change things in one script, and not over many, many webages. As a simple example, say I have this:

aI("text=Home;url="http://www.msn.com";status=MSN;");

and I have a function called getLoc("Name") that returns a string when called. (I simplified this, so ignore that the passed variable is pointless right now)

function getLoc(Name) {
Loc = "http://www.msn.com";
return Loc;
}
Now, why won't this work?

aI("text=Home;url=javascript:getLoc('MSN');status=MSN;");

It creates a blank page with "http://www.msn.com" on it. That's it. Am I doing something stupid here? (Quite possibly!) Is it not possible? Can anyone help?
User avatar
Hergio
Milonic God
Milonic God
Posts: 1123
Joined: Wed Jun 12, 2002 7:46 pm
Location: Rochester, NY

Post by Hergio »

I dont think that would work either if you just did it in plain sight on a page as in <a href='javascript:foo(bar);'> and expect it to return a string and have it navigate to that string. By putting the "javascript:" in an <a> tag you are telling the browser, "I know this is a hyperlink, but forget about going anywhere, just do this function". So your browser listens and just outputs what you return. To fix this, in your function you need to say



Code: Select all

function getLoc(Name) {




   Loc = "http://www.msn.com";




   if( ie ){

       location.href = Loc;    //<-- This will get your browser to go somewhere

   }

   if( ns4 )

   {

      //do whatever it is netscape needs to navigate somewhere

   }

}
Dave Hergert
Software Engineer
"Helping to make the menu better, one :?: at a time."
Morat
Beginner
Beginner
Posts: 2
Joined: Thu Aug 28, 2003 6:33 pm

Post by Morat »

Yes, that makes sense. I'm not terribly experienced at this, but I'm the only one in the department that's ever done any sort of javascript or ASP work. So I'm stuck. Thanks!

Yes, that does work. It makes the whole system more complicated, but it's still considerably better than the alternatives. Thank you!
User avatar
Hergio
Milonic God
Milonic God
Posts: 1123
Joined: Wed Jun 12, 2002 7:46 pm
Location: Rochester, NY

Post by Hergio »

It shouldnt make it any more complicated because you said you had to resolve the URL anyways (by whatever means you guys are doing it) but then instead of returning the URL, you just call location.href on it...you can also pass querystrings through this method as well...its perfectly fine to say location.href = "page.asp?var1=foo". Its just like pasting something into the URL box and hitting return.
Anyways, you're welcome! :)
Dave Hergert
Software Engineer
"Helping to make the menu better, one :?: at a time."
Post Reply