-3

I have a url http://local.evibe-dash.in/tickets/123?ref=avl-error#10610

I want to get ref=avl-error and 10610 in two variable

for Example somthing like that

var1 = 'avl-error';
var2 = 10610;

How can I get it in javascript or jQuery

Mohammad
  • 21,175
  • 15
  • 55
  • 84
Vikash
  • 3,391
  • 2
  • 23
  • 39

3 Answers3

1

Using regex in .match() you can find part of string you want.

var url = "http://local.evibe-dash.in/tickets/123?ref=avl-error#10610";
var query = url.match(/ref=([^#]+)/)[1];
var hash = url.match(/#([^#]+)/)[1];
console.log(query, hash);
Mohammad
  • 21,175
  • 15
  • 55
  • 84
  • I want "ref=avl-error", not only avl-error for query . how can I get it. – Vikash Sep 27 '16 at 12:10
  • @Vikash Use `var query = url.match(/ref=[^#]+/)[0]` instead it. – Mohammad Sep 27 '16 at 12:14
  • Thanks @Mohammad . Solved. can you please suggest me some good regex tutorials, My problem has solved but I didn't understand code completely. – Vikash Sep 27 '16 at 12:21
  • @Vikash There are many turorial and answer in web that you can find them by search. However you can test any regex code in [regex101](https://regex101.com/) tool. – Mohammad Sep 27 '16 at 12:24
  • Thanks @mohammad. I really appreciate your help :) – Vikash Sep 27 '16 at 12:32
0

This was asked and answered multiple times on Stack.

Look at this posts:

How can I get query string values in JavaScript?

Parse query string in JavaScript

Basicly you have to:

  1. read query string: all after "?"
  2. split string with "&" char into array
  3. split all array elements with "="
  4. decode values with decodeURIComponent

Please use stack before asking. And give some reputation to people on the links above.

Community
  • 1
  • 1
elrado
  • 4,960
  • 1
  • 17
  • 15
0

Try this function you get an array with query string key, it's value and the string after hash

var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
    sURLVariables = sPageURL.split('&'),
    sParameterName,
    i; 
for (i = 0; i < sURLVariables.length; i++) {
    sParameterName = sURLVariables[i].split('=');

    if (sParameterName[0] === sParam) {
        sParameterName[1] === undefined ? true : sParameterName[1];
    }
   if(window.location.href.indexOf("#") > -1) {
    var url = window.location.href;
    sParameterName[2] = url.match(/#([^#]+)/)[1];
        }
      }
     return sParameterName;
     };

    var a = getUrlParameter('your-query-string-key');
    console.log(a);
Owais Aslam
  • 1,577
  • 1
  • 17
  • 39