I have written a jQuery program and inserted it into my Page1.aspx file.
js-url.js resides in the Scripts file in the base folder.
I have a website coming into this page: http://www.mypage.com/Page1.aspx?var1=123&var2=987 This jQuery code is suppose to extract the variables var1 and var2 from the url and place them into TextBox1 and TextBox2 respectively.
I am not sure if js-url.js is the correct script. If someone knows a better or the same way to extract these variables please show me.
<script src="../Scripts/js-url.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
var GET1=$("#TextBox1");
var GET2=$("#TextBox2");
GET1.val($.url("?var1"));
GET2.val($.url("?var2"));
});
</script>
Do you need more functionality within the js-url.js file? A jQuery plugin seems like overkill for this feature. I found a reusable function in pure javascript that supposedly does the trick pretty well:
which is just works and compact (not event a kb)...
or, u can simply use a string manipulation technique to get the querystring
function GetQueryStringValue (strKey) (
var url = window.location.search;
var querystringStr = url.substring(url.indexOf("?"));
var keys = querystringStr.split('&');
for(i=0 ; i< keys.length; i++) {
var key = keys[i].split('=')[0];
var value = keys[i].split('=')[1];
if(strKey == key) {
return value
}
}
}
hope this helps...
Cheers!
KK
Please mark as Answer if post helps in resolving your issue
My Site
function GetQueryStringParams(sParam)
{
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}
Yes, with javascript you can refer the follow code:
var first = getUrlVars()["id"];
var second = getUrlVars()["page"];
alert(first);
alert(second);
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
Please mark the replies as answers if they help or unmark if not.
Feedback to us
Philosophaie
Member
5 Points
82 Posts
Extracting variables from a url
Feb 26, 2013 10:36 PM|LINK
I have written a jQuery program and inserted it into my Page1.aspx file.
js-url.js resides in the Scripts file in the base folder.
I have a website coming into this page:
http://www.mypage.com/Page1.aspx?var1=123&var2=987
This jQuery code is suppose to extract the variables var1 and var2 from the url and place them into TextBox1 and TextBox2 respectively.
I am not sure if js-url.js is the correct script.
If someone knows a better or the same way to extract these variables please show me.
<script src="../Scripts/js-url.js" type="text/javascript"></script> <script language="javascript" type="text/javascript"> $(document).ready(function() { var GET1=$("#TextBox1"); var GET2=$("#TextBox2"); GET1.val($.url("?var1")); GET2.val($.url("?var2")); }); </script>AceCorban
Star
12320 Points
2270 Posts
Re: Extracting variables from a url
Feb 26, 2013 10:41 PM|LINK
Do you need more functionality within the js-url.js file? A jQuery plugin seems like overkill for this feature. I found a reusable function in pure javascript that supposedly does the trick pretty well:
http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values
function getParameterByName(name) { name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); var regexS = "[\\?&]" + name + "=([^&#]*)"; var regex = new RegExp(regexS); var results = regex.exec(window.location.search); if(results == null) return ""; else return decodeURIComponent(results[1].replace(/\+/g, " ")); }kedarrkulkar...
All-Star
34433 Points
5535 Posts
Re: Extracting variables from a url
Feb 27, 2013 04:39 AM|LINK
does this script work fine? if yes, then u can go ahead with it
there are few more plugins to read querystring values... like this
http://code.google.com/p/m-jq-projects/wiki/rwQueryString_jQuery
which is just works and compact (not event a kb)...
or, u can simply use a string manipulation technique to get the querystring
function GetQueryStringValue (strKey) (
var url = window.location.search;
var querystringStr = url.substring(url.indexOf("?"));
var keys = querystringStr.split('&');
for(i=0 ; i< keys.length; i++) {
var key = keys[i].split('=')[0];
var value = keys[i].split('=')[1];
if(strKey == key) {
return value
}
}
}
hope this helps...
KK
Please mark as Answer if post helps in resolving your issue
My Site
prabu.raveen...
Contributor
5020 Points
955 Posts
Re: Extracting variables from a url
Feb 27, 2013 04:54 AM|LINK
Try as below,
function GetQueryStringParams(sParam) { var sPageURL = window.location.search.substring(1); var sURLVariables = sPageURL.split('&'); for (var i = 0; i < sURLVariables.length; i++) { var sParameterName = sURLVariables[i].split('='); if (sParameterName[0] == sParam) { return sParameterName[1]; } } }GET1.val(GetQueryStringParams('var1'));GET2.val(GetQueryStringParams('var2'));Song-Tian - ...
All-Star
43697 Points
4304 Posts
Microsoft
Re: Extracting variables from a url
Feb 27, 2013 07:51 AM|LINK
Hi,
You can use query string. The code as follow:
string t=Request.QueryString["name"];
More details, please refer to: http://msdn.microsoft.com/en-us/library/system.web.httprequest.querystring.aspx.
Feedback to us
Develop and promote your apps in Windows Store
AceCorban
Star
12320 Points
2270 Posts
Re: Extracting variables from a url
Feb 27, 2013 03:14 PM|LINK
I think he's looking for a Javascript solution.
Song-Tian - ...
All-Star
43697 Points
4304 Posts
Microsoft
Re: Extracting variables from a url
Feb 28, 2013 01:10 AM|LINK
Hi,
Yes, with javascript you can refer the follow code:
function getUrlVars() { var vars = {}; var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) { vars[key] = value; }); return vars; }Feedback to us
Develop and promote your apps in Windows Store