Click here to Skip to main content
13,899,680 members
Rate this:
 
Please Sign up or sign in to vote.
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

This is how you can get URL of current page:
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:
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
   
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
$.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
   
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.
Aarthi.33 28-Jun-16 10:27am
   
as of now it should be tested in local with harcoded values could you please help
Sergey Alexandrovich Kryukov 28-Jun-16 11:01am
   
I don't think it make any practical sense, especially if you "have no idea", but I gave you some code in Solution 2. It it what you need?
—SA
Aarthi.33 28-Jun-16 11:23am
   
Hi Sergey thank you.... I have no idea but there is other team who would decide it but as of now for testing purpose I need to implement as hardcoded values in javascript to login to the application
Sergey Alexandrovich Kryukov 28-Jun-16 11:25am
   
I don't think it makes sense. Nothing should be passed via query string; and the worst idea is to pass credentials.

I formally answered your question on getting URL query parameter; gave you fully operational code; will you accept the answer formally now?

—SA

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.190306.1 | Last Updated 28 Jun 2016
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100