Back To Start Of Archive
Taken From The Forum: Help & Support for DHTML Menu Version 5+
Forum Topic: Click to view post
Last Updated: Saturday July 14 2012 - 06:07:16
Question regarding Javascript functions and the url string
Poster: Morat
Dated: Thursday August 28 2003 - 18:51:23 BST
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?
Poster: Hergio
Dated: Thursday August 28 2003 - 19:09:40 BST
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:
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
}
}
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
}
}
Poster: Morat
Dated: Thursday August 28 2003 - 19:26:25 BST
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!
Poster: Hergio
Dated: Friday August 29 2003 - 13:37:20 BST
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!