Techin Techin - 7 months ago 46
Javascript Question

get token from more 1 type by javascript

I have 4 type of url and need to get token.

type1 http://gettoken/index.php?action=test&token=aaaaa

type2 http://gettoken/index.php?action=test&token=aaaaa&login=admin

type3 http://gettoken/index.phpaction=test&login=admin&token=aaaaa&expires=3600

type4 http://gettoken/index.php?action=test&login=admin&expires=3600&token=aaaaa

and token length 5 character.

Answer Source

Here the function which will return any parameter value from url

function getParameterByName(name, url) {
if (!url) 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, " "));

Use it like bellow

var url ="http://gettoken/index.php?action=test&login=admin&token=aaaaa&expires=3600";
var action = getParameterByName('action',url); // "test"
var login = getParameterByName('login',url); // "admin"
var token = getParameterByName('token',url); // "aaaaa"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download