Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need to pass the querstring through a URL and retrieve the Querystring value from the URl and pass the value to a existing method in javascript.

What I have tried:

$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
var someValue = $('#message').val();
var url = 'https://www.google.co.in?Empid=' + someValue;
window.open(url);
)};
Posted
Updated 28-Jun-16 4:55am

This is how you can get URL of current page:
JavaScript
var url = window.location; // object, not a string
var urlString = window.location.href;

This is how you can parse this string to get an object with key-value pairs representing query parameters:
JavaScript
function parseQuery(url) {
   var result = {};
   var parameters = window.location.search;
   // returns query string after '?', which can be empty
   if (!parameters) return result; 
   parameters = parameters[1].split("&");
   for (var parameter in parameters) {
      var keyValue = parameters[parameter].split("=");
      result[keyValue[0]] = decodeURIComponent(keyValue[1]);
   }
   return result;
}

// ...

// usage:
// let's assume the url is http://www.myDomain.tld?first=1&second=2
var test = parseQuery(window.location.href);
var first = test.first; // returns 1
var second = text.second; // returns 2
// same thing:
first = test["first"]; // returns 1
second = text["second"]; // returns 2

Yes, I tested it before posting. :-)

Note that my function does not take into account all pathological cases. You can use it as it, then you may deal with undefined objects returned as values, and so on. Or you can add some check-ups, according to your assumptions on the query string and your requirements.

[EDIT] I improved my code sample according to the suggestions by Richard Deeming in his comments to the answer, see below.

—SA
 
Share this answer
 
v4
Comments
Richard Deeming 28-Jun-16 12:30pm    
You could use location.search to extract the querystring without having to split the entire URL, and without having to worry about the anchor.

You'll probably also want to call decodeURIComponent on each value to remove any URL-encoding.
Sergey Alexandrovich Kryukov 28-Jun-16 16:01pm    
Good ideas. I didn't think about it. I improved code according to these suggestions and credited them — thank you very much.
—SA
Hi Aarthi.33,
When I tried above script and used in my script as is it gave me error. I think the parenthesis are not matching.
It looks like you want to use $(‘#message’).val() as query string. If so it works if you use following script
JavaScript
$.connection.hub.start().done(function () {
        $('#sendmessage').click(function () {
            var someValue = $('#message').val();
            var url = 'https://www.google.co.in?Empid=' + someValue;
            window.open(url);
        });
    });

Hope that helps
 
Share this answer
 
Comments
Aarthi.33 28-Jun-16 9:51am    
Hi the code i have given works fine.. yes parenthesis is missing i need to implement harcoding the log in username and password in javascript. please help me
Sergey Alexandrovich Kryukov 28-Jun-16 10:06am    
This is a wrong idea; such password "protection" would make no sense; it cannot protect anything.
—SA
Aarthi.33 28-Jun-16 10:10am    
hi its for testing purpose in local not for production
Sergey Alexandrovich Kryukov 28-Jun-16 10:12am    
All right, but why password then? May I know what do you want to do in production?
—SA
Aarthi.33 28-Jun-16 10:17am    
i have no idea how it should be implemented in production
i'm completely new to this kind of application and no idea how it should be done in javascript.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900