0

I am passing the value of username into the query string but i am unable to get the value of the name in jquery.

this is my url

https://dev.hudpowerbid.com/front-end-pm/?fepaction=newmessageShami%20Kothari

I want to get only Shami Kothari

this is how i am attaching the name with the query string

var name= jQuery(this).parent().siblings(".htb200").html();
window.location.href ='https://example.com/front-end-pm/?fepaction=newmessage'+name;

this is how i am attaching name with url now i want to get this in jquery

this is how I am getting the value.

function getUrlVars() {
var url = 'https://example.com/front-end-pm/?fepaction=newmessage'+name;
var vars = {};
var hashes = url.split("?")[1];
var hash = hashes.split('&');

for (var i = 0; i < hash.length; i++) {
params=hash[i].split("=");
vars[params[0]] = params[1];
}
return vars;
}
1
  • @NickParson I think i need a separator in the url but when i am passing the & or ? separator i am getting syntax error Commented Mar 10, 2019 at 14:19

1 Answer 1

1

Your issue is that you are not assigning a key "name" to the value "Shami Kothari" in your query string, instead you are currently just appending it to the end of another value. To add the key "name" to your query string you can use & to specify another key-value pair:

window.location.href ='https://example.com/front-end-pm/?fepaction=newmessage&name='+name;

This way when you try and get name, it will look for the key "name" and retrieve the value, which is "Shami Kothari"

See working example below:

function getUrlVars() {
  var name = "Shami Kothari";
  var url = 'https://example.com/front-end-pm/?fepaction=newmessage&name=' + name;
  var vars = {};
  var hashes = url.split("?")[1];
  var hash = hashes.split('&');

  for (var i = 0; i < hash.length; i++) {
    params = hash[i].split("=");
    vars[params[0]] = params[1];
  }
  return vars;
}

console.log(getUrlVars());

Sign up to request clarification or add additional context in comments.

6 Comments

let me try with this
@TayyabGulsherVohra ok, hope it all goes well :)
Uncaught TypeError: Cannot read property 'split' of undefined
when i have used like this var hashes = url.split("&")[1];
@TayyabGulsherVohra did you see the code snippet I added to my answer? Don’t change anything in your code expect for adding the “&name” + name in
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.