-2

I want to get values from URL through GET method and then want to show those values on right top corner of my header area.

function GetUrlValue(variable)
 {    
    var SearchString = window.location.search.substring(1);
    var VariableArray = SearchString.split('&');
        for (var i = 0; i < VariableArray.length; i++)
        {
            var KeyValuePair = VariableArray[i].split('=');
            if (KeyValuePair[0] == variable)
            {
                return KeyValuePair[1];
            }
        }
}
MANOJ GOPI
  • 1,279
  • 10
  • 31
Mahesh Kalyankar
  • 107
  • 1
  • 1
  • 13
  • What exactly are you asking? Are you having trouble with the code you posted or want to know how to get the results of this code into the top right corner of your page? – cspete Jan 15 '15 at 11:50
  • this code is working properly but i want to get username and email out of my url and want to show same on right top corner of my header, like we show username after user logedin. – Mahesh Kalyankar Jan 15 '15 at 12:03
  • I have a user registration form, after submitting form, i want to take username and email id out of that URL then want to show that on header area, so how can i do that any help – Mahesh Kalyankar Jan 15 '15 at 12:17

1 Answers1

0

Your code above wouldn't get the first querystring value only the subsequest ones as you split on & and the first qs value would follow a ?

Try the code below. I've used one of the answers from this post to get the querystring values and added some basic HTML and CSS

function getParameterByName(name) {
    
    //var url = window.location.search
    var url = 'http://example.com?email=myname@email.com&username=myusername';
    
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(url);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

$( document ).ready(function() {  
    var email = getParameterByName('email');
    var username = getParameterByName('username');
    $("#userDetails").text(email + ' ' + username)
});
#header{
    top:0;
    left:0;
    right:0;
    position: absolute;
    height:30px;
    width:100%;
    background-color:blue;
}

#userDetails {
    float: right;
    color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="header">
    <div id="userDetails"></div>
</div>
Community
  • 1
  • 1
cspete
  • 170
  • 8