stop web browser going to website when javascript called

Hi hope you can help:

i need a command from when someone clicks a button it sends a parameter added to url. I have managed the adding parameter and i can send to url but it sends the browser window to the url: i really what it to stay on page

code i have is

window.location.href  = "http://localhost:1323/api/1/voip?staff_pin="+ number.text() +"&action=checkout"; 

Open in new window


this works but it does take the browser window away for index.html:   need it just to send/post the url

Thanks in advance

James
LVL 31
James MurrellProduct SpecialistAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
You have to use AJAX for that.  No version of 'window.location' will do that.  Tutorial here: http://www.w3schools.com/ajax/default.asp
James MurrellProduct SpecialistAuthor Commented:
thanks i had looked there but did not fully get it

i think i need http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp but could not make it work
James MurrellProduct SpecialistAuthor Commented:
i tried

			xmlhttp.open("GET","http://localhost:1323/api/1/voip?staff_pin="+ number.text() +"&action=checkout",true);
			xmlhttp.send();

Open in new window


but it did not work
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Dave BaldwinFixer of ProblemsCommented:
No, it's not that simple.  You need the whole example as shown on this page:  http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first  You just don't need the 'myDiv' div and button though you may keep the 'myDiv' at first to display your test response because there is Always some response.
Rainer JeschorCommented:
Hi,
if you need to support various browsers (and versions e.g. IE8) I would suggest to use here the jQuery library, as this will wrap and support nearly all browsers (with version 1.11.x).
http://jquery.com/
e.g.
$.get("http://localhost:1323/api/1/voip?staff_pin="+ number.text() +"&action=checkout", function( data ) {
  if (window.console) console.log("Response was " + data );
});

Open in new window

Just my 2ct.

Rainer
James MurrellProduct SpecialistAuthor Commented:
hi thanks for this but it must be my lack of programming..... i just dont get it....

cannot post all code: but here is what i can

/* ==========================================================================
   main.js
   ========================================================================== */
var $j = jQuery.noConflict();

$j(function(){

    var dials = $j(".dials ol li");
    var index;
    var number = $j(".number");
    var total;
	var pinLength = 4;
	var xmlhttp;
	

    dials.click(function(){
		
		index = dials.index(this);
		
		if (number.text().length == pinLength && index < 10)
		{
			alert("***************");
			return;
		}
	
		if (number.text().length < pinLength && (index == 10 || index == 11))
		{
			alert("**************************************");
			return;
		} 
		
		if(index == 9){

            number.append("0");
                    )else if(index == 11){
			
			
			if(confirm("Please confirm you wish to check Out"))
			//alert("check Out" +number.text() +"Test getting number");
			window.location.href  = "http://localhost:1323/api/1/voip?staff_pin="+ number.text() +"&action=checkout"; 

Open in new window

Dave BaldwinFixer of ProblemsCommented:
I didn't realize you were using jQuery.  It has it's own version of AJAX.  See http://api.jquery.com/jQuery.ajax/ but you may also want to "Request Attention" above.  I rarely use jQuery.

In any case, it will not be as simple as "window.location.href" because that will always do what you are seeing now.
Rainer JeschorCommented:
Hi,
OK, you are already using jQuery - excellent.
Then the following should work:
	/* ==========================================================================
   main.js
   ========================================================================== */
var $j = jQuery.noConflict();

$j(function(){

    var dials = $j(".dials ol li");
    var index;
    var number = $j(".number");
    var total;
	var pinLength = 4;
	var xmlhttp;
	

    dials.click(function(){
		
		index = dials.index(this);
		
		if (number.text().length == pinLength && index < 10)
		{
			alert("***************");
			return;
		}
	
		if (number.text().length < pinLength && (index == 10 || index == 11))
		{
			alert("**************************************");
			return;
		} 
		
		if(index == 9){

            number.append("0");
                    )else if(index == 11){
			
			
			if(confirm("Please confirm you wish to check Out")) {
				//alert("check Out" +number.text() +"Test getting number");
				//window.location.href  = "http://localhost:1323/api/1/voip?staff_pin="+ number.text() +"&action=checkout"; 
				$j.get("http://localhost:1323/api/1/voip?staff_pin="+ number.text() +"&action=checkout", function( data ) {
					if (window.console) console.log("Response was " + data );
					alert('Request should be sent');
				});
			}
   ...

Open in new window

I added both an alert as well as a console.log, so that you can see the response.

HTH
Rainer

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.