Javascript function to strip user ID from URL - javascript

i need a javascript function to strip the user ID from a given URL.
for example. i have this URL http://www.example.com/member.php?id=100001877097904
how can i retrieve the ID of the user from that URL?
Thanks in advance.

function getParameterByName(url, name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(url);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}

One possible solution:
var url = "http://www.example.com/member.php?id=100001877097904",
id = (url.match(/[?&#]id=(\d+)/) || []).pop();
console.log(id);

Related

How to write regexp to get a paramater from URL

I need help with a regexp to get only the code parameter from this url:
URL: code=4%2FsAB_thYuaw3b12R0eLklKlc-qcvNg6f8E8pgvu_02MTTJE0NOcyvXkrTQrB3QK8209wSbIpLDEBrk8vUGYKQ41I&scope=https:.....
My Code:
getParameterByName(name) {
var url = window.location.href;
name = name.replace(/[\[\]]/g, '\\$&');
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$|)'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
this piece of code is returning `
4/sAB_thYuaw3b12R0eLklKlc-qcvNg6f8E8pgvu_02MTTJE0NOcyvXkrTQrB3QK8209wSbIpLDEBrk8vUGYKQ41I
In this case i need the first and only backslash to be a % sign instead..
I can't get it to work.
Thnx in advance!
It works for me!!!
function getUrlParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
Untested JavaScript regex:
var myRe = /=(.*)&/igm
var url = 'code=4%2FsAB_thYuaw3b12R0eLklKlc-qcvNg6f8E8pgvu_02MTTJE0NOcyvXkrTQrB3QK8209wSbIpLDEBrk8vUGYKQ41I&scope=https:';
var code = myRe.exec(url);
Use \bcode=([^&]+). It's sure to grab code query using word boundary 'code=' then capture any characters until it reaches & or the end.
test regex here: https://regex101.com/r/eSnB4S/1

Pagination with Javascript Went Wrong

I've coding pagination with JS
out like this
<a id="prev">Previous Page</a>
<a id="next">Next Pages</a>
and JS Code like this
$('#next').click(function(){
var url = window.location.href;
var urllen = url.length;
var cur = parseInt((url.substr(urllen-1)).substr(0,1));
var nurl = url.substr(0,(urllen-1))+(cur+1);
if(cur=="NaN") { window.location = (url); }
else { window.location = (nurl); }
});
$('#prev').click(function(){
var url = window.location.href;
var urllen = url.length;
var cur = (url.substr(urllen-1)).substr(0,1);
if(cur==1||cur=="NaN") { window.location = (url); }
else { var nurl = url.substr(0,(urllen-1))+(cur-1); window.location = (nurl); }
});
and my url like
http://localtest/rftpages/record.html?s=1&l=1&bike_id=1
let's me explain the reason that i'm using a JavaScript method is i don't want to change my URL that containing page variable that i use in my whole page
so what i'm doing is get all the URL and change bike_id value to next/prev
and the problem is when it count to 19 or URL like
http://localtest/rftpages/record.html?s=1&l=1&bike_id=19
then i goes next again the URL will become
http://localtest/rftpages/record.html?s=1&l=1&bike_id=110
any idea/suggestion to fix this ?
Thanks
What you should do is grab the page from the query string and the either increment or decrements it based on what is clicked.
all you need is this function to get the parameters:
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
So if I assume your example:
http://localtest/rftpages/record.html?s=1&l=1&bike_id=19
Then you can change your function to be:
$('#next').on("click", function() {
var currentPageParameter = getParameterByName("bike_id");
var s = getParameterByName("s");
var l = getParameterByName("l");
var myPage = parseInt(currentPageParameter);
if (! isNaN(myPage )) {
myPage = myPage + 1;
window.location = location.protocol + '//' + location.host + location.pathname + "?s=" + s + "&l=" + l + "&bike_id=" + myPage;
}
});
$('#prev').on("click", function() {
var currentPageParameter = getParameterByName("bike_id");
var s = getParameterByName("s");
var l = getParameterByName("l");
var myPage = parseInt(currentPageParameter);
if (! isNaN(myPage )) {
myPage = myPage - 1;
window.location = location.protocol + '//' + location.host + location.pathname + "?s=" + s + "&l=" + l + "&bike_id=" + myPage;
}
});

Trying to extract information from querystring

I'm new to JavaScript and I am trying to extract information from a query string on the web I created, if I load straight on the page without the query string the page will load but once I redirect from my form page to the page where I'm doing the parsing it freezes and crashes... can anyone help please! :(
http://main.xfiddle.com/7d679c3a/Project1/Commission.php
http://main.xfiddle.com/7d679c3a/Project1/contactForm.php
http://main.xfiddle.com/7d679c3a/Project1/DiceRoll.php
http://main.xfiddle.com/7d679c3a/Project1/IsEven.php
http://main.xfiddle.com/7d679c3a/Project1/palindrome.php
http://main.xfiddle.com/7d679c3a/Project1/part1.php
http://main.xfiddle.com/7d679c3a/Project1/passwordStrength.php
http://main.xfiddle.com/7d679c3a/Project1/allinOne.php
JavaScript Code
var $ = function(id)
{
return document.getElementById(id);
}
var formInfo = location.search();
formInfo = formInfo.substring(1, formInfo.length);
while (formInfo.indexOf("+") != -1)
{
formInfo = formInfo.replace("+", " ");
}
while (formInfo.indexOf("=") != -1)
{
formInfo.replace("=", " ");
}
formInfo = decodeURI(formInfo);
formInfo.replace("firstname", "");
formInfo.replace("lastname", "");
formInfo.replace("phonenumber", "");
formInfo.replace("postalcode", "");
formInfo.replace("startingmoney", "");
var infoArray = formInfo.split("&");
var firstName = infoArray[0];
var lastName = infoArray[1];
var phoneNumber = infoArray[2];
var postalCode = infoArray[3];
var startingMoney = infoArray[4];
$("playername").innerHTML = firstName + " " +lastName;
$("playerinfo").innerHTML = phoneNumber + " " + postalCode;
$("money").innerHTML = " $$" + startingMoney;
HTML Code
<div id="fireinfo">
<p id="playername"></p><br/>
<p id="playerinfo"></p>
<p id="money"></p>
</div>
I want to out put the information i get from the query string into the player name, player info and money id's.
Use this function to get query string value:
function getQueryString(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(window.location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
};
var firstname = getQueryString('firstname');

Get auth token from URL

I am trying to get a Twitter oauth token using the below string. How can I "run" the string and then get the token?
This is the request string:
https://api.twitter.com/oauth/request_token?oauth_consumer_key=9TL0JGKTIv7GyOBeg8ynuxg&oauth_nonce=Xty48&oauth_signature=3skps99e6zkn0rcUGadVUEuHFon4%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1346603336
This is the result:
oauth_token=TBFdNoytaizrfMAWNZ6feqNz3BsozHk5AesIioX8u8Ec&oauth_token_secret=DtQ3jiUIeVdRcBAKwVQJRWpgKtEHi3m1ylk0nlsHCBj0&oauth_callback_confirmed=true
See answer to this question. It demonstrates how to get a value from the querystring.
Here, I've altered it to take the name value collection as a parameter:
function getParameterByName(name, qs)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(qs);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
Usage:
var result = "oauth_token=TBF...&oauth_token_secret=DtQ...";
var token = getParameterByName("oauth_token", result);

redirect page by HyperLink1

Script
function getParam(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 unescape(results[1]);
}
HTML
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="~/PersonPage/ConfighMessages.aspx?idCompany=javascript:getParam('idCompany');">bbb</asp:HyperLink>
i would like if idCompany=123 in url onclikc this HyperLink redirect to PersonPage/ConfighMessages.aspx?idCompany=123
but this code redirect to:/PersonPage/ConfighMessages.aspx?idCompany=getParam('idCompany');
javascript:getParam('idCompany') is not going to work.
Try:
$("#HyperLink1").click(function(){
window.location.href = "~/PersonPage/ConfighMessages.aspx?idCompany="+idCompany;
});
$(<%= "#"+HyperLink1.ClientID %>).attr("href","/PersonPage/ConfighMessages.aspx?idCompany="+ getParam('idCompany'));
Or
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%= "~/PersonPage/ConfighMessages.aspx?idCompany=" + Request.QueryString["idCompany"]%>'>bbb</asp:HyperLink>

Categories

Resources