How can I define a function in JavaScript? -


how define function? want define name1 in fb.api > name: name1, got error in console

uncaught referenceerror: name1 not defined 

here javascript code:

function share(platform) {     var percent = $('.progress .percent').html().replace('%', '');     var names = $('.progress .names').html().touppercase().split(' & ');     var name1 = names[0];     var name2 = names[1];      var querystring =   encodeuricomponent('name1='+name1+'&name2='+name2+'&percent='+percent+'&text='+text+'&lang='+lang);     // var json = json.stringify(querystring);     var share_params = base64_encode(querystring);      var share_url = 'http://example?';      var url = 'https://www.facebook.com/sharer/sharer.php?u='+encodeuricomponent(share_url)+share_params;      if(platform == 'whatsapp') {         var share_title = name1+' + '+name2+' = '+percent+'%! '+window.famobi.__('sharetext');         var url = 'whatsapp://send?text='+encodeuricomponent(share_title);     }      window.open(url,'name','height=400,width=600');     return false; } 

and

function sharefb(countr,share,platform) {     $("div.bs-example").empty();     $("p#share").html('<div class="col-lg-12 text-center"><div class="progress progress-striped active"><div id="statusbar"><div class="progress-bar progress-bar-info" role="progressbar" aria-valuenow="20" aria-valuemin="0" aria-valuemax="100" style="width:100%;"><span class="sr-only">20% complete</span></div></div></div></div>');     statusbar();     var formdata = $("input#cache").val();     var formdatav = $("input#cache").val();     if (countr != "") {         var formdata = formdatav.substr(0, formdatav.indexof((countr + 1) + "."));         if (formdata == "") {             var formdata = $("input#cache").val();         }      }     fb.api('/me/feed', 'post', {         message: formdata,         name: name1,         description: "test des",         caption: "test app",         link: "https://apps.facebook.com/*****/",         picture: "http://examplepic.png"     }, function(response) {         console.log(response);         if (!response || response.error) {             alert('error occured');         }         $("p#share").empty();     }); } 

few ways this. easiest: stick before function share(platform) {:

var name1; 

then change var name1 = names[0]; to

name1 = names[0]; 

this defines global var name1 (first bit) , makes sure don't recreate local variable (second bit).

so becomes:

var name1;  function share(platform) {     var percent = $('.progress .percent').html().replace('%', '');     var names = $('.progress .names').html().touppercase().split(' &amp; ');     name1 = names[0]; 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -