Solved

Jquery - Access to the function

Posted on 2013-01-21
2
191 Views
Last Modified: 2013-02-01
Hi E's, I have this code:
$("#tela_inicial").click(function(){
em_jogo = 1;

$(function(){
        $.ajax({
        type: "POST",
        url: "abertura.php",
        data: {"nj":numero_jogo, "fase":'1', "ref":ref_puzzle, "ip":ip_cliente},
        success: function(dat){
                if(dat == ""){
                    //nao faz nada
                } else {
                // redirect
                window.location=dat;
                } 
        }
        });
});

Open in new window

That snippet code shows a function that is executed when click in the div (#tela_inicial).
My issue is, I want access to that code, but in other context.
In practice, if I click in #tela_iniciar, the script execute code inside the function.
I have a part in my code where I will need access to that code, but not with the click in #tela_iniciar.
So, my question is, how I access to that function without via #tela_iniciar?

The best regards, JC
0
Comment
Question by:Pedro Chagas
2 Comments
 
LVL 8

Accepted Solution

by:
soupBoy earned 500 total points
ID: 38801677
Try putting your 'ajax call' into its own function....then you can call it whenever you need to...

$("#tela_inicial").click(function(){
	em_jogo = 1;

	aberturaAjax();
});

$('someSelector').on('someOtherEvent', function(){
  //lets call the ajax function again!
  abertuaAjax();
});
    
function abertuaAjax(){
	$(function(){
    	$.ajax({
        	type: "POST",
            url: "abertura.php",
            data: {"nj":numero_jogo, "fase":'1', "ref":ref_puzzle, "ip":ip_cliente},
            success: function(dat){
                if(dat == ""){
                    //nao faz nada
                } else {
                    // redirect
                    window.location=dat;
                } 
          }
	});
}

Open in new window

0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 38801755
Hi @soupBoy, what's in the box code does not matter, is just for this example, in the future can be lots of kinds of code.
What matter here, is the access to the same function, but in different way. For my case not matter what is inside the function.
It may not have solution.

~JC
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now