Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jun 2011
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    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"];
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •