Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-11-2011, 10:39 PM   PM User | #1
sketchgal
New Coder

 
Join Date: Jun 2011
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
sketchgal is an unknown quantity at this point
Question how to get url variable values

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.

Code:
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.
sketchgal is offline   Reply With Quote
Old 07-12-2011, 06:10 AM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
From snipplr.com (http://snipplr.com/view/799/get-url-variables/)
Code:
// 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"];
}
devnull69 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:28 PM.


Advertisement
Log in to turn off these ads.