2009-08-06

JAVA Get URL Parameters Using Javascript

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];
}


The way that the function is used is fairly simple. Let's say you have
the following URL:

http://www.foo.com/index.html?bob=123&frank=321&tom=213#top

You want to get the value from the frank parameter so you call the
javascript function as follows:

var frank_param = gup( 'frank' );

0 留言: