Mengambil variabel type GET dengan Javascript

misal ada url = dicoba.html?namavariabel=tesdicoba
 


<script language="javascript">

var GETDATA = new Array();

// Get the string that follows the "?" in the window's location.
var sGet = window.location.search;
if (sGet) // if has a value...
{
    // Drop the leading "?"
    sGet = sGet.substr(1);
    
    // Generate a string array of the name value pairs.
    // Each array element will have the form "foo=bar"
    var sNVPairs = sGet.split("&");
    
    // Now, for each name-value pair, we need to extract
    // the name and value.
    for (var i = 0; i < sNVPairs.length; i++)
    {
        // So, sNVPairs[i] contains the current element...
        // Split it at the equals sign.
        var sNV = sNVPairs[i].split("=");
        
        // Assign the pair to the GETDATA array.
        var sName = sNV[0];
        var sValue = sNV[1];
        GETDATA[sName] = sValue;
    }
}
  
var namarum = GETDATA["namavariabel"];
  //Run some code here
//alert(namarum + " ?");
var namavar = "";
if(typeof(namarum) != "undefined")
{ 
//alert('a='+namarum); 
namavar= namarum;
}
alert(namavar);
</script>
Advertisements

2 comments

    1. coba kopas source nya mas, lalu simpan dengan nama test.html

      bis itu jalanin di browser test.html itu setelah itu coba tambahkan url nya dengan “?namavariabel=tesdicoba” jadi contoh urlnya ” ……/test.html?namavariabel=tesdicoba”

      setelah itu jalankan, silahkan mencoba 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s