如何获取href值并将其作为javascript变量?

I have a list of link like this:

<li><a href="landing.php?products=IPCamera&id=1">IP Camera 1</a></li>
<li><a href="landing.php?products=IPCamera&id=2">IP Camera 2</a></li>
<li><a href="landing.php?products=CCTVCamera&id=1">CCTV 1</a></li>

and the following sample php code:

// PRODUCTS AND ID IS DYNAMICALLY PRODUCE FROM PHP QUERY
$sql = "SELECT * FROM tablename";
$result = mysql_query($sql); //
foreach($result as $field) {
   $listlink = "landing.php?products=".$field[products]."&id"=$field[id];
   $output = "<li><a href=\"$listlink\">".$field[menuname]."</a></li>";
}
echo $output; // THIS IS THE RESULT OF LIST OF LINK ABOVE

My question is how to make the value of products and id to become a javascript variable and pass it to a jquery ajax to call a php processor.

thanks

Use the location.search to get the query part of the current location and then extract the field to use.

Hope that helps.

If you're talking about code running on this products.php page based on the products and id in the URL:

<script type="text/javascript">

var params = {}, queryString = location.search.substring(1),
    re = /([^&=]+)=([^&]*)/g, m;

while (m = re.exec(queryString)) {
    params[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
}

//now params is an object containing all the query string parameters from the URL
do_your_ajax_thing(params[id], params[products]);

</script>

You can use data attributes and access them via jquery by using the jQuery.data() function.

$sql = "SELECT * FROM tablename";
$result = mysql_query($sql); //
foreach($result as $field) {
   $listlink = "landing.php?products=".$field[products]."&id"=$field[id];
   $output = "<li><a data-products=\"$field[products]\" data-id=\"$field[id]\"  href=\"$listlink\">".$field[menuname]."</a></li>";
}

the js part will be like this

$("a").click(function() {
   var id = $(this).data('id');
   var products = $(this).data('products');
   var url = $(this).attr('href');

   // your ajax request goes here
});

You can put all the values of products column in a row in PHP and then use json_encode() to send the array json encoded to javascript. Just an example how to do this.

<html>
<head>
<script type="text/javascript">
var productValueArray = <?php

               $outputArray = array();
               $sql = "SELECT * FROM tablename";
               $result = mysql_query($sql); //
               foreach($result as $field) {
                  array_push($outputArray, $field[menuname]);
               }
               echo json_encode($outputArray);

                        ?>

    </script>
    </head>
    <body>
    </body>
</html>
var urlArgs = location.search,
    products = urlArgs.substring(urlArgs.indexOf("=") + 1, urlArgs.indexOf("&")),

and for id also you can use substring() or split() method and do the string manipulation.