0

I am trying to build a jQuery web page , that when called from the browser (GET request) will get an id as parameter.

For example lets say that the web page written in jQuery is the mypage.com . I want from my browser now to be able to pass an id to this page like this : mypage.com/thisIsMyId. By writting this in my browser i want the mypage.com web page to open and the thisIsMyID parameter to be stored in a variable in my jQuery script for using it later.

Is this possible? I ve never used jQuery before. I ve only used php to send json/xml files with post requests. Is it possible to pass a parameter like this in the URL and make jQuery page catch the parameter for later use?

How would this look in jQuery? Just the line that gets the parameter and saves it in a variable.

EDIT


To make it simpler to understand how it works... How would the html page look , that when requested from the browser like mypage.com/thiIsMyId would just print me the thisIsMyId ? I guess this would be html and jQuery right?

2 Answers2

1

This is perfectly possible but you will need a server-side rewrite rule in addition to client-side JS. This will also be rather horrible to do, so you might consider using the hashtag for it - easier to pass around, and does not get to the server.

Both principles are the same. Let's assume you are using the URI (which is the harder of the two). The JS variable representing the URL is window.location, which is an object castable to a string. There is no need for jQuery to do this. In order to get anything after the last slash, the following works:

 window.location.toString().substr(window.location.toString().lastIndexOf("/")+1);

You will then need to write the code that you want to use this ID for, however.

The hashtag is simpler:

 window.location.hash.substr(1);

This will give you anything after the #.

Note: real GET variable would be mydomain.com/?id=4

Sébastien Renauld
  • 19,203
  • 2
  • 46
  • 66
  • Sebastian thank you very much for your answer. I am bit confused though. Whats the difference between using "/" and "#" ? Your answer confused me a bit. Actually i dont really care if is "/" or "#" or "?" separating the id from the url. I just want a way to get the parameter in my html page. take a look in my edit too. – LolaEnaMilo May 15 '13 at 15:43
  • `mydomain.com/#1` is a hashtag reference. The server gets queried for `mydomain.com/` - the `#1` remains on the client side. `mydomain.com/1` goes whole to the server. That's what the difference is - pick what you prefer and use whichever function to parse the location. – Sébastien Renauld May 15 '13 at 15:44
  • aaah!! yeap now makes a lot of sense. – LolaEnaMilo May 15 '13 at 15:47
  • Correct me if i am wrong but the correct hashtag reference would be mydomain.com#1 and not mydomain.com/#1 . It seems that if i use the "/" , server queries all the string. – LolaEnaMilo May 15 '13 at 16:03
  • no no i dont mean that. Read again my comment. When i try in my local server : myPage.html/#1 , i get that the page was not found. When i try mypage.html#1 then mypage.html is loaded – LolaEnaMilo May 15 '13 at 16:04
  • Yes, if you use /, server attempts to fetch it all. That's the point of the hashtag and why I recommended it: **it allows you to keep everything client-side**, allows users to cache your unique page, and generally smoothes things out. – Sébastien Renauld May 15 '13 at 16:06
  • I wrote my comment in response to your first comment. Fix it there by removing the "/" for future readers. – LolaEnaMilo May 15 '13 at 16:07
  • Can't edit that comment anymore - it has been 25 minutes. People will read the chain. – Sébastien Renauld May 15 '13 at 16:11
0

The GET would look like this mypage.com?id=2

And to get the GET variable from the url all you need to do is this...

var urlVariable = [];
urlVariable = location.search.replace('?', '').split('=');

urlVariable[0] would be the name of the name and urlVariable[1] would be the value

Hope this helps.

Josh Balcitis
  • 490
  • 6
  • 19