Vikash Vikash - 2 months ago 8
jQuery Question

How to get query and hash from url using javascript?

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

Answer

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);