Reply
Grabbing Variables from URL
Old 04-05-2006, 07:37 AM Grabbing Variables from URL
lothop's Avatar
Extreme Talker

Posts: 229
Hiyas,

I've downloaded a Dynamic Menu which uses JS. What I'm wondering is how to pull variable information through JS.

Eg.

URL is : index.php?p1=page1&p2=page2

How do I pull what p1 and p2 equals to input it into the JS code?
The JS creates the links on the Dynamic Menu with
Code:
menus[0].addItem("index.php?p1=disclaimer", "", 100, "center", "Disclaimer", 0);
Wondering how I can change it to
Code:
menus[0].addItem("index.php?p1=disclaimer&p2= ~urlvariable~ &p3= ~urlvariable~", "", 100, "center", "Disclaimer", 0);
So basically, I can change one variable at a time through a link. A different link.
Code:
menus[0].addItem("index.php?p1= ~urlvariable~ &p2= ~contact~ &p3= ~urlvariable~", "", 100, "center", "Disclaimer", 0);
Any help on this would be amazing, I've looked around :/ Maybe ive searched for the wrong things.

Thanks,
Greg
__________________
CUrrent sites working on...

http://www.Crashdays.com/
http://www.nzfiles.com/

Last edited by lothop : 04-06-2006 at 03:52 AM.
lothop is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 04-05-2006, 09:00 PM Re: Grabbing Variables from URL
Super Talker

Posts: 144
well, i can't read that code, but here's a method for ya...

Code:
location.getVar = function(getVar) {
  var oRegExp = new RegExp(getVar +"=([^&.]+)", "i");
  if (!oRegExp.test(this.href)) return false;
  oRegExp.exec(this.href);
  return unescape(RegExp.$1);
}
 
alert(location.getVar("p1"));
__________________
create.vibe
web development portfolio
createvibe.com is offline
Reply With Quote
View Public Profile Visit createvibe.com's homepage!
 
Old 04-06-2006, 04:08 AM Re: Grabbing Variables from URL
lothop's Avatar
Extreme Talker

Posts: 229
Sorry about that was a bit small lol.

I have tried to implement that code, but it doesn't want to work correctly. The tutorial page included with the download says.

1) This parameter defines the link that the item can go to upon being clicked. This can be a local link, or a global link like "http://www.yahoo.com". You can also add Javascript in by coding "javascript:code" into the parameter. If you use a string inside the Javascript code segment, you'll have to surround the string like so: "javascript:alert('Alerting')"

menus[0].addItem("#", "", 100, "center", "Admin", 6); this being the code which creates the link, where the link is housed in ("#")
__________________
CUrrent sites working on...

http://www.Crashdays.com/
http://www.nzfiles.com/
lothop is offline
Reply With Quote
View Public Profile
 
Old 04-06-2006, 04:23 AM Re: Grabbing Variables from URL
Super Talker

Posts: 144
u are right, the regular expression doesn't like more than 1 param in the query string. hold tight, I'll fix it in a bit. another option is to remove the ? and split by & then split each iteration by = ...
__________________
create.vibe
web development portfolio
createvibe.com is offline
Reply With Quote
View Public Profile Visit createvibe.com's homepage!
 
Old 04-06-2006, 07:34 AM Re: Grabbing Variables from URL
Super Talker

Posts: 144
doh! the url was escaped... that's why.

this should work for ya...

Code:
location.getVar = function(get_var) {
  var regexp = new RegExp(get_var +"=([^&.]+)", "i");
  if (regexp.exec(unescape(this.href)))
    return(RegExp.$1);
  else
    return(null);
}
 
alert( location.getVar("my_url_var") );
__________________
create.vibe
web development portfolio
createvibe.com is offline
Reply With Quote
View Public Profile Visit createvibe.com's homepage!
 
Old 04-07-2006, 02:22 AM Re: Grabbing Variables from URL
lothop's Avatar
Extreme Talker

Posts: 229
Hrmm, still not working

When I input the code, it doesn't show the menu.
I have included my pages in the attachment. If you could pleeease take a look at it I would be very grateful. If you don't want to, thats fine aswell.

Thanks for all your help so far, it apreciated.

Greg.
Attached Files
File Type: zip mywebsite.zip (94.0 KB, 5 views)
__________________
CUrrent sites working on...

http://www.Crashdays.com/
http://www.nzfiles.com/
lothop is offline
Reply With Quote
View Public Profile
 
Old 04-07-2006, 02:39 AM Re: Grabbing Variables from URL
Super Talker

Posts: 144
well, looking in your config.js file, I see the Init() function is what you are working with? ... next time try narrowing it down to 1 file?

Code:
menus[1].addItem("javascript:alert(location.getVar(my_url_var))", "", 22, "left", "Brand New", 0);
you pretty much just copied the code. the alert was used as an example, you were supposed to change it up for your script... unless you are saving 'my_url_var' with the value of your variable, that's just not going to work.

with this example,

index.php?p1=page1&p2=page2

to grab the 'p1' variable from the url query string, we would do...

Code:
 
var p1 = location.getVar("p1");
so, with having that in mind (and not using a declared variable as we did above),

Code:
 
menus[1].addItem(location.getVar("p1"), "", 22, "left", "Brand New", 0);
the above code would put 'page' into that menu item.

of course if we wanted to declare all the url variables ahead of time you need...
  1. know the list of variables you are expecting
  2. take a different approach for an unknown amount of variables
we can cover the 2nd approach later if need be, but to pre-define the variables (as in the url 'index.php?p1=page1&p2=page2') we would simply code:

Code:
 
var p1 = location.getVar("p1");
var p2 = location.getVar("p2");
and then simply use those in the addItem method.

Code:
 
menus[0].addItem(p1, yadda, yadda);
menus[0].addItem(p2, yadda, yadda);
REMEMBER, of course, to declare the 'location.getVar' method somewhere in the scope of your script otherwise, you will get an error, 'no such property or method' or something like it.

here is the method once more...

Code:
location.getVar = function(get_var) {
  var regexp = new RegExp(get_var +"=([^&.]+)", "i");
  if (regexp.exec(unescape(this.href)))
    return(RegExp.$1);
  else
    return(null);
}
hope that clears things up for ya.
__________________
create.vibe
web development portfolio
createvibe.com is offline
Reply With Quote
View Public Profile Visit createvibe.com's homepage!
 
Old 05-02-2006, 06:33 AM Re: Grabbing Variables from URL
lothop's Avatar
Extreme Talker

Posts: 229
Thank you very much for your help createvibe.com

I know you must think I was lazy, just not 100% sure on variables and javascript.
Just different to what I'm used to, so kind of hit a brick wall.

I ended up using PHP.
I outputted the code(config.js) using my functions page and just seperating the javascript code and using
PHP Code:
"index.php?p='.$_GET[p1].'&p2='.$_GET[p2].'&p3='.$_GET[p3].'" 
to place the variable from the URL into place.

Thanks for your time, was helpful eventhough I found another way

Greg
__________________
CUrrent sites working on...

http://www.Crashdays.com/
http://www.nzfiles.com/
lothop is offline
Reply With Quote
View Public Profile
 
Old 05-03-2006, 12:46 AM Re: Grabbing Variables from URL
Super Talker

Posts: 144
no worries. that's a better approach
__________________
create.vibe
web development portfolio
createvibe.com is offline
Reply With Quote
View Public Profile Visit createvibe.com's homepage!
 
Reply     « Reply to Grabbing Variables from URL
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.16616 seconds with 13 queries