To load javascript asynchronously, If we have latest browser where HTML5 supported then we need only to add "async" tag having the value true.
In case of having old browser, we need to create a js function that will add our javascript asynchronously.
Please Follow the Javascript function example below :-
function loadScriptAsync (scriptFilePath){
var scriptHeadTag=document.getElementsByTagName('script')[0];
var ss=document.createElement('script');
ss.type='text/javascript';
ss.async=true;
ss.src= scriptFilePath
scriptHeadTag.parentNode.insertBefore(ss,s);
}
loadScriptAsync('/js/jsfile.js');
Now you have to Pass PHP Array into the Ajax Call.
//Setp 1: Include jQuery file in your page
$phpData = array('fname'=>'G', 'lname'=>'Geo','email'=>'g7789@gmaill.com'); //Setp 2: This is your PHP Array data
[script]
/** Setp 3: This is Ajax Call will be called after page fully load **/
function saveuserdetail(mydata) {
$.ajax({
type: "POST",//Here you can use POST/GET Method
url: '/postpageurl/',
data: mydata,
success: function(msg){
console.log('Data'+msg);
},
dataType: 'json'
});
}
/** This is Ajax Call will be called after page fully load **/
/* Step 4: When Page Load, PHP Array will convert into javaScript Object */
/* Pass the javaScript Object into javaScript Function i.e saveuserdetail**/
$( document ).ready(function() {
var data=new Object("echo json_encode($phpData) ")
saveuserdetail(data);
});
[/script]
0 Comment(s)