-3

I'm new to web development, any idea about this. I want to get the parameter i passed in URL. I tried some functions but no luck. Any Help will be appreciated. Thanks in Advance

  • What functions did you try? What did they do that was different to what you expected? Can you give an example of what you're trying to achieve? – Shadow Nov 03 '17 at 00:52
  • Can you give an example of a url with the expected parameters please? This sort of question has also been answered many times. If the parameters are a `query` you can use `window.location.search` or if the url contains a `hash` you can use `window.location.hash` I would recommend you open the browser console and paste `window.location` and open the result to see the properties available for you to use in order to access what you wish to use. – NewToJS Nov 03 '17 at 00:52
  • 1
    Possible duplicate of [How to get the value from the GET parameters?](https://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-get-parameters) – MrMarlow Nov 03 '17 at 00:53

1 Answers1

1

You can use regex on your function

    function get_parameter(keyword, your_url) {
if (!your_url) your_url = window.location.href;
keyword = keyword.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + keyword + "(=([^&#]*)|&|#|$)"),
    results = regex.exec(your_url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}

and this is how you call the function

var parameter = get_parameter('sample');

I used that code for a long time :) and i also saw that code thru internet but i dont remember where so i cant give any credits :D hope it will help you

  • Fast response because this is a copy/paste of an existing answer which could be found with some research. I will not be up voting for the reason this question should be marked as duplicate rather than trying to take credit for someone else's response. – NewToJS Nov 03 '17 at 00:55
  • NewToJS he just said that he didnt own the code and he only saw on the internet. What are you up to? – Martin Luxxe Nov 03 '17 at 01:02
  • A comment with a link to the duplicate question would be the correct thing to do. By copying/pasting the answer and then asking for it to be marked it taking credit for something he/she didn't solve. Comments are there for this exact reason along with privileges gained via reputation. If **Kimson** would be kinda enough to at least add a link to the original source and place some comments in the source code to explain the step taken to achieve this then this answer would be more acceptable. Some people want to learn from solutions rather than just copy/pasting. – NewToJS Nov 03 '17 at 01:04
  • No Problem. but keep in mind to search deeper first next time before asking question here because not all people will tolerate you and they will give you downvotes for asking duplicate questions – Kimson Magat Nov 03 '17 at 01:06
  • Hello NewToJS. i just said " i dont remember where so i cant give any credits " coz i used that code for a long time. how can i give the link? – Kimson Magat Nov 03 '17 at 01:08
  • @KimsonMagat Well could you at least add some comments to help future visitors. As I have said in my previous comment, some people would like to understand the source code - how it works and each step taken to achieve the end result. Since the OP didn't provide any source code when answering question like this a detailed description should be added. If you give a detailed answer with comments to explain the steps taken I will be more than happy to up vote the answer as it will be useful to others in the future. – NewToJS Nov 03 '17 at 01:11
  • i dont need upvote or whatever :) i just want to help. besides the code is readable and understandable enough, right? – Kimson Magat Nov 03 '17 at 01:16
  • @KimsonMagat Correct, I find the source code very readable and understandable but that's because I know how that source code works and know the results of the properties targeted/called but for those who don't know comments and a description is more **helpful** which is what you wanted to do right....? **help** people by providing that source code. Also I would recommend you declare `your_url` properly as it's causing a syntax error in my browser when pasting your source code into the console. **Example:** `var your_url = window.location.href;` – NewToJS Nov 03 '17 at 01:20