...

View Full Version : how to get url variable values



sketchgal
07-11-2011, 10:39 PM
Can someone please tell me how I would modify the following function to get it to check if a url variable is present and if so to add it into the string variable for the url.


function ajaxFunction(pagenum){
var ajaxRequest; // The variable that makes Ajax possible!

try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var drop_1 = document.getElementById('drop_1').value;
var model = document.getElementById('model').value;
var mileage = document.getElementById('mileage').value;
var colour = document.getElementById('colour').value;
var age = document.getElementById('age').value;
var min_price = document.getElementById('min_price').value;
var max_price = document.getElementById('max_price').value;
var min_engine_size = document.getElementById('min_engine_size').value;
var max_engine_size = document.getElementById('max_engine_size').value;
//var pics = document.getElementById('pics').value;
if (document.getElementById("pics").checked==true) {
var photos = "Yes";
}
else {
var photos = "No";
}
var keywords = document.getElementById('keywords').value;
var queryString = "?drop_1=" + drop_1 + "&model=" + model + "&mileage=" + mileage + "&colour=" + colour + "&age=" + age + "&min_price=" + min_price + "&max_price=" + max_price + "&min_engine_size=" + min_engine_size + "&max_engine_size=" + max_engine_size + "&photos=" + photos + "&keywords=" + keywords + "&page=" + pagenum;
ajaxRequest.open("GET", "filtered.php" + queryString, true);
ajaxRequest.send(null);
}

If my url is index.php?make=Apolo then I need to get this added into the var queryString part of the function so that the ajax returns the correct values.

Any help would really be appreciated, I'm fairly new to js.

devnull69
07-12-2011, 06:10 AM
From snipplr.com (http://snipplr.com/view/799/get-url-variables/)


// Read a page's GET URL variables and return them as an associative array.
function getUrlVars() {
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

for(var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}

return vars;
}

// check to see if a certain key is present
var myKeys = getUrlVars();
if(myKeys["key"]) {
alert('key is present, value is ' + myKeys["key"]);
// add to a querystring
querystring += '&key=' + myKeys["key"];
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum