Pass Query string to WebMethod in Javascript [duplicate] - javascript

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Get query string values in JavaScript
What is the best way to get "test1" from
http://localhost:3311/blabl/allprofiles.aspx?username=test1
, and through PageMethod pass it to webmethod. I think one way is to take from window.location.pathname, cut the string and pass it like a parameter.

May be you can use only javascript like:
var search = function(){
var s = window.location.search.substr(1),
p = s.split(/\&/),
l = p.length,
kv, r = {};
if(l === 0){return false;}
while(l--){
kv = p[l].split(/\=/);
r[kv[0]] = kv[1] || true;
}
return r;
}();
Then use in your code search.username

Try
string username = Request.QueryString["username"];
In a PageMethod, you can do
string username = HttpContext.Current.Request.QueryString["username"];

Related

A function on checking if the string is a url in Javascript [duplicate]

This question already has answers here:
Check if a JavaScript string is a URL
(36 answers)
Closed 9 years ago.
I am writing a function to check if the input string is a url in Javascript. Should I use substring(0,6) and see if starts with "http://"? Or there is a better way to achieve? Cheers.
Something like this should handle the simple cases:
function is_url(url) {
return Boolean(url.match(/^https?:\/\//));
}
You could use a regular expression
/^http:\/\//.test(urlString)
You could use:
if(myvalue.indexOf('https://') == 0 || myvalue.indexOf('http://') == 0)
Depends how detailed you want to get with it. I am sure you can find a regex that would do it on here is you searched around.
With regex:
/^http:/.test("http://example.com/")
If you wanted to check www too: /^(http:|www\.)/.test("http://example.com/")
And to be different:
function matchString(str,matches)
{
if(matches)
{
matchString.toCheck=matches;
}
var matched = [];
for(var i=[0,str.length];i[0]<i[1]; i[0]++)
{
for(var j=[0,matchString.toCheck.length];j[0]<j[1]; j[0]++)
{
if(!matched[j[0]])matched[j[0]]={c:0,i:-1};
if(matchString.toCheck[j[0]][matched[j[0]].c]==str[i[0]])
{
matched[j[0]].c++;
if(matched[j[0]].i==-1)matched[j[0]].i=i[0];
}
else if(matchString.toCheck[j[0]].length!=matched[j[0]].c)matched[j[0]]={c:0,i:-1};
}
}
return matched;
}
var urlVariants = matchString("https://",["http://","https://","www."]);
var isUrl = false;
for(var i=[0,urlVariants.length]; i[0]<i[1]&&!isUrl; i[0]++)
{
isUrl = (urlVariants[i[0]].i==0);//index at the start
}
console.log(isUrl);
I think regex is a better solution:
function isAnUrl(url){
var expression = /[-a-zA-Z0-9#:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9#:%_\+.~#?&//=]*)?/gi;
var regex = new RegExp(expression);
if (url.match(regex))
return true;
else return false;
}

How to remove $ from javascript variable? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Remove characters from a string
I have a variable p which I would like to remove the $ from. This variable will be a number such as $10.56. How can I do this? I thought it could be done using .replace('$','') but i'm not quite sure how to implement this.
Here is my javascript code:
function myFunction() {
var p = parseFloat(document.getElementById('p_input').value);
var q = parseFloat(document.getElementById('q_input').value);
if (!q){
document.getElementById('t').value = '';
}
else {
var t = q * p;
document.getElementById('t_output').value = t;
}
}
It's pretty simple:
var myString = "$15.62"
console.log(myString.replace('$', ''));
//Logs: "15.62"
Please note that this new value is not actually "saved" to myString, you'll have to assign it to a variable, yourself:
var newString = myString.replace('$', '');
Try this, assuming that the values of p_input and q_input will be the money values:
var p = parseFloat(document.getElementById('p_input').value.replace('$', ''));
var q = parseFloat(document.getElementById('q_input').value.replace('$', ''));

Getting Url Parameters as Javascript variables [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to get GET and POST variables with JQuery?
Get query string values in JavaScript
lets say my site has url http://www.akbrowser.tk/ds/?q=http://www.chess.com&r=http://www.blackle.com
(the two parameters are URLs)
I now want to get two javascript variables on the site, with the values of the two urls. (so the first variable would be the chess.com, and the second would be blackle.com [of course it would have the http and all, but I can only post one hyperlink])
how would I do that?
I saw some other similar questions on this site, and the poster gave a long solution that I didn't understand (I think it had something to do with find a '=' and take everything after it) but in this case it would give "http:// www.chess.com&r=http://www.blackle.com [without the space]" as one of the variables.
I also saw another post with multiple parameters like mine, but the poster gave a long solution so since I didn't understand it, I couldn't really make it do what I wanted it to do.
so can someone help me?
Try this function:
function getQueryParam(href, paramName) {
var query = href.substring(href.indexOf('?')+1);
var params = query.split('&');
for(var i = 0; i < params.length; i++) {
var param = params[i].split('=');
if(param.length > 1) {
if(param[0] == paramName) {
return param[1];
}
}
}
return null;
}
console.log(getQueryParam('http://www.akbrowser.tk/ds/?q=http://www.chess.com&r=http://www.blackle.com', 'r'));
function gup(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results == null)
return "";
else
return results[1];
}
var qString = gup("q");
var rString = gup("r");
What this does is do a regex to find whatever [name] you pass in to the function.

read parameter on js file [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Pass vars to JavaScript via the SRC attribute
May I know how to read get the p value on js file link like filename.js?p=value with local javascript in the js file? Any function to work like the $_GET['p'] in php? Thanks.
try this:
var tmp = location.href, it;
if (q) it = tmp.substr(q + 1).split('&');
else it = '';
for (var i in it) {
var t = it[i].split('=');
if (t[0] == 'p') {
//do something
break;
}
}
function _GET( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
This will do the equivalent in javascript.

how to check "contains" using jquery [duplicate]

This question already has answers here:
How to check whether a string contains a substring in JavaScript?
(3 answers)
Closed 9 years ago.
I tried of checking contains option using jquery for the birthday but its getting exception
var _dob = "4/10";
// this line doesn't work
var _adob = _dob.contains('/') ? _dob.Split('/') : _dob.Split('-');
$('#Month').val(_adob[0]);
$('#Day').val(_adob[1]);
but i can't able to split.. its resulting in error on getting _adob itself
Try this:
var _dob = "4/10";
var _adob;
if (_dob.indexOf("/") >-1) {
_adob = _dob.split("/");
} else {
_adob - _dob.split("-");
}
Direct Answer
indexOf(something)>-1
var _dob = "4/10";
var _adob = _dob.indexOf('/')>-1 ? _dob.split('/') : _dob.split('-');
$('#Month').val(_adob[0]);
$('#Day').val(_adob[1]);
Indirectly
You really don't need to check that the string contains that... Using a regular expression, you can split on a -, /, or . by building a character set:
var _dob = '4.10';
var _aodb = _dob.split(new RegExp('[-/.]'));

Categories

Resources