-2

Hi i am new in javascript before i read the case split using the function. I just followed but not understand it. Can you guys give me a link or guide to explain how it work? tks a ton

var first = getUrlVars()["id"];
var second = getUrlVars()["page"];

function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}
Cao Linh Truong
  • 253
  • 8
  • 20

2 Answers2

1

This function return the value of each varrian from url.

In your code, you want to get id, page from url. I guest you have a url like : your-page?id=value&page=value, and you want to get them, don't you?

You need to read the replace function at http://www.w3schools.com/jsref/jsref_replace.asp

Nguyen Linh
  • 266
  • 2
  • 8
0

There is a kind of function you would like. Hope you will understand this :

var getUrlVars = function( url ){
    if( !url.match( /\?/ ) ) return {};
    var paramsfull = url.replace( /^.*\?/, "" ).split( /\&/g );
    var params = {};
    var _temp;
    for( var p in paramsfull ){
        _temp = paramsfull[ p ].split( /\=/ );
        params[ _temp[ 0 ] ] = _temp[ 1 ];
    }
    return params;
}

var first = getUrlVars( window.location.href )[ "id" ];
var second = getUrlVars( window.location.href )[ "page" ];
karkael
  • 431
  • 2
  • 9