javascript - jquery: making button.click & json call work together but keeping them separated -


i have contact form encrypts form message:

<script type="text/javascript" src="jquery-1.10.2.min.js"></script>  <form name="form_contact" method="post" action="/cgi/formmail.pl">     // other input fields here      <textarea name="message" id="message" required></textarea>      <button id="sendbutton" type="submit">send</button> </form> 

the following javascript script works , things form message when people click on send-button:

$(document).ready(function() {     $("button[id$='sendbutton']").click(function(){      //check if message has been encrypted or empty       var = document.form_contact.message.value.indexof('-----begin pgp message-----');       if((i >= 0) || (document.form_contact.message.value === ''))         {             document.form_contact.submit(); return;         }       else         {             document.form_contact.message.value='\n\n'+ document.form_contact.message.value + "\n\n\n\n\n\n\n\n" + "--------------------------" + "\n"              if (typeof(navigator.language) != undefined && typeof(navigator.language) != null) {                 document.form_contact.message.value=document.form_contact.message.value + '\n'+ "language: " + (navigator.language);}             else if (typeof(navigator.browserlanguage) != undefined && typeof(navigator.browserlanguage) != null) {                 document.form_contact.message.value=document.form_contact.message.value + '\n'+ "language: " + (navigator.browserlanguage); }              // , here's geoip service data should appended form message              addgeoipdata();              //finally resulting message text encrypted             document.form_contact.message.value='\n\n'+doencrypt(keyid, keytyp, pubkey, document.form_contact.message.value);         }     }); });    function addgeoipdata(){         $.get('http://ipinfo.io', function(response)         {                         $("#message").val( $("#message").val() + "\n\n" + "ip: "+ response.ip  + "\n" + "location: " + response.city + ", " + response.country);         }, 'jsonp'); }; 

well, works except: it not add response geoip service ipinfo.io form message before encrypting it.

i saw jquery json call example elsewhere puts all code inside $.get('http://ipinfo.io', function(response){...}) that's not want.
if goes wrong ipinfo query nothing else work - because it's inside $.get('http://ipinfo.io', function(response){...}).

in other words: how can make button.click , $.get-json call work script works keep them separate (json outside button.click) if json call fails reason button click function , in still work?

i have marked position in javascript results of json call supposed appended form message.

thank help.


edit:

after 1bn hours of trial & error, stumbled across way make work:

so put geoipinfo query separate script gets info when page loading.

$.getjson("https://freegeoip.net/json/", function (location) {  var results = "\n\n" + "ip: "+ location.ip  + "\n" + "location: " + location.city + ", " + location.region_name + ", " + location.country_name;       window.$geoipinfo = results;  }); 

and in other script posted earlier, add variable $geoipinfo form message by

document.form_contact.message.value=document.form_contact.message.value + §geoipinfo; 

it seems $geoipinfo global variable , therefore can use contents outside function , in other scripts.

i don't care long works maybe tell me if solution complies rules of javascript.

the jquery api: http://api.jquery.com/jquery.get/

specifies can put handler in .always() , called whether succeeds or fails.

$.get('http://ipinfo.io', , function(response)     {                     $("#message").val( $("#message").val() + "\n\n" + "ip: "+ response.ip  + "\n" + "location: " + response.city + ", " + response.country);     }, 'jsonp').always(function(){         document.form_contact.message.value='\n\n'+doencrypt(keyid, keytyp, pubkey, document.form_contact.message.value);     }); 

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -