Click here to Skip to main content
15,900,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im using the below mentioned script to retrive the querystring value But i'm getting the ERROR "ParseQuery is undefined"

What I have tried:

var test = parseQuery(window.location.href);
var SSN = test.SSN; // returns 1
// var second = text.second; // returns 2
// same thing:
// first = test["SSN"]; // returns 1
// second = text["second"]; // returns 2
$(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;
Posted
Updated 1-Jul-16 0:20am

 
Share this answer
 
Comments
Aarthi.33 1-Jul-16 7:45am    
Thank you Karthik :-)
I think it's because you have defined the method after your method call.
Try this :-

$(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;
});

var test = parseQuery(window.location.href);
var SSN = test.SSN; // returns 1
console.log(SSN);
// var second = text.second; // returns 2
// same thing:
// first = test["SSN"]; // returns 1
// second = text["second"]; // returns 2


This is giving the perfect result for me.
Hope this one will help you :).

UPDATE
$(function () {
    //Set the hubs URL for the connection
    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]);
            if (result[keyValue[0]] == null)
                alert("The parameter is null");

            else 
                alert(resultkeyValue[1]);
        }

        return result;
    };

    var test = parseQuery(window.location.href);
    var SSN = test;
});


Remove $ from the method function parseQuery(url) { and try again.
 
Share this answer
 
v2
Comments
Aarthi.33 1-Jul-16 6:13am    
Hi Still i'm getting the same error
Aarthi.33 1-Jul-16 6:14am    
<!DOCTYPE html>
<html>
<head>
<title>SignalR Simple Chat</title>
<style type="text/css">
.container {
background-color: #99CCFF;
border: thick solid #808080;
padding: 20px;
margin: 20px;
}
</style>
</head>
<body>
<div class="container">
<input type="text" id="message" />
<input type="button" id="sendmessage" value="Send" />
<input type="hidden" id="displayname" />
<ul id="discussion"></ul>
</div>
<!--Script references. -->
<!--Reference the jQuery library. -->
<script src="Scripts/jquery-1.6.4.min.js"></script>
<!--Reference the SignalR library. -->
<script src="Scripts/jquery.signalR-2.0.3.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="http://localhost:8080/signalr/hubs"></script>
<!--Add script to update the page and send messages.-->
<script type="text/javascript">
$(function () {

//Set the hubs URL for the connection
$.connection.hub.url = "http://localhost:8080/signalr";

// Declare a proxy to reference the hub.
var chat = $.connection.myHub;

// Create a function that the hub can call to broadcast messages.
chat.client.addMessage = function (name, message) {
// Html encode display name and message.
var encodedName = $('<div />').text(name).html();
var encodedMsg = $('<div />').text(message).html();
// Add the message to the page.
$('#discussion').append('<li>' + encodedName
+ '
:  ' + encodedMsg + '</li>');
};
// Get the user name and store it to prepend to messages.
$('#displayname').val(prompt('Enter your name:', ''));
// Set initial focus to message input box.
$('#message').focus();
// Start the connection.
$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
//location.href = "www.google.com";
var someValue = $('#message').val();
var url = 'https://www.google.co.in?SSN=' + someValue;
window.open(url);
// Would write the value of the QueryString-variable called name to the console
//chat.server.send($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();

});

$(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]);
if (result[keyValue[0]] == null)

alert("The parameter is null");

else alert(resultkeyValue[1]);

}
return result;
});

var test = parseQuery(window.location.href);
var SSN = test
Aarthi.33 1-Jul-16 6:15am    
This is the code im using to pass a ssn value and get it from the url and display the passed vale using if (result[keyValue[0]] == null)

alert("The parameter is null");

else alert(resultkeyValue[1]);


this above code under the below mentioned function

$(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]);
if (result[keyValue[0]] == null)

alert("The parameter is null");

else alert(resultkeyValue[1]);

}
return result;
});
Aarthi.33 1-Jul-16 6:15am    
but i dont see it happening :-(
Prava-MFS 1-Jul-16 6:23am    
Updated the code, please check and try once.

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