|
Mise à jour : 07/12/2010
Difficulté :
Intermédiaire
6 849 visites depuis 7 jours,
dont 273 sur ce chapitre
classé 33/786
|
![]() Méthode | ![]() Internet Explorer | ![]() Firefox | ![]() Opera | ![]() Google Chrome | ![]() Safari |
|---|---|---|---|---|---|
| Dynamic Script Loading | Oui |
Oui |
En partie Obligation de charger un fichier .js |
Oui |
Oui |
1 2 3 4 5 | <script type="text/javascript"> <!-- document.write("<script type=\"text\/javascript\" src=\"external.js\"><\/script>"); //--> </script> |
1 2 3 4 | var DSLScript = document.createElement("script"); DSLScript.src = "DynamicScriptLoading_1.js"; DSLScript.type = "text/javascript"; document.body.appendChild(DSLScript); |
1 2 3 | function callback(sMessage) { alert(sMessage); } |
1 | callback("Hello world !"); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Dynamic Script Loading</title> <script type="text/javascript"> <!-- function send() { var DSLScript = document.createElement("script"); DSLScript.src = "DynamicScriptLoading_1.js"; DSLScript.type = "text/javascript"; document.body.appendChild(DSLScript); document.body.removeChild(DSLScript); } function callback(sMessage) { alert(sMessage); } //--> </script> </head> <body> <input type="button" value="Envoyer la requête" onclick="send()" /> </body> </html> |
.1 | DynamicScriptLoading_2.php?variable1=valeur1&variable2=valeur2 |
1 | DSLScript.src = "DynamicScriptLoading_2.php?Pseudo=Thunderseb&Prenom=Sebastien"; |
1 2 3 4 5 6 | <?php header("Content-type: text/javascript"); ?> var sMessage = "Bonjour <?php echo $_GET['Prenom'] ?>, alias <?php echo $_GET['Pseudo'] ?> !"; callback(sMessage); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | function send(sUrl, oParams) { for (sName in oParams) { if (sUrl.indexOf("?") != -1) { sUrl += "&"; } else { sUrl += "?"; } sUrl += encodeURIComponent(sName) + "=" + encodeURIComponent(oParams[sName]); } var DSLScript = document.createElement("script"); DSLScript.src = sUrl; DSLScript.type = "text/javascript"; document.body.appendChild(DSLScript); document.body.removeChild(DSLScript); } |
1 2 3 4 5 | var oParams = { "Pseudo": "Thunderseb", "Prenom": "Sebastien", "callback": "messageFromServer" }; |
1 2 3 4 5 | <?php header("Content-type: text/javascript"); ?> var sMessage = "Bonjour <?php echo $_GET['Prenom'] ?>s alias <?php echo $_GET['Pseudo'] ?> !"; <?php echo $_GET['callback'] ?>(sMessage); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | var oSoftwares = { "Adobe": [ "Acrobat", "InDesign", "Photoshop" ], "Macromedia": [ "Dreamweaver", "Flash", "FreeHand" ], "Microsoft": [ "Office", "Visual C++", "Windows Media Player" ] }; callback(oSoftwares); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | function callback(oJson) { var tree = "", nbItems; for (sItem in oJson) { tree += sItem + "\n"; nbItems = oJson[sItem].length; for (var i=0; i<nbItems; i++) { tree += "\t" + oSoftwares[sItem][i] + "\n"; } } alert(tree); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Techniques AJAX - Dynamic Script Loading - JSON Statique</title> <script type="text/javascript"> <!-- function send(sUrl) { var DSLScript = document.createElement("script"); DSLScript.src = sUrl; DSLScript.type = "text/javascript"; document.body.appendChild(DSLScript); document.body.removeChild(DSLScript); } function callback(oJson) { var tree = "", nbItems; for (sItem in oJson) { tree += sItem + "\n"; nbItems = oJson[sItem].length; for (var i=0; i<nbItems; i++) { tree += "\t" + oSoftwares[sItem][i] + "\n"; } } alert(tree); } function getInfo() { send("DynamicScriptLoading_JSON_1.js"); } //--> </script> </head> <body> <p id="main"> <input type="button" value="Récupérer les données" onclick="getInfo();" /> </p> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php header("Content-type: text/javascript"); echo 'var oSoftwares = { "Adobe": [ "Acrobat", "InDesign", "Photoshop" ], "Macromedia": [ "Dreamweaver", "Flash", "FreeHand" ], "Microsoft": [ "Office", "Visual C++", "WMedia Player" ] };'; echo $_GET['callback'] ?>(oSoftwares); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Techniques AJAX - Dynamic Script Loading - JSON Dynamique</title> <script type="text/javascript"> <!-- function sendDSL(sUrl, oParams) { for (sName in oParams) { if (sUrl.indexOf("?") != -1) { sUrl += "&"; } else { sUrl += "?"; } sUrl += encodeURIComponent(sName) + "=" + encodeURIComponent(oParams[sName]); } var DSLScript = document.createElement("script"); DSLScript.src = sUrl; DSLScript.type = "text/javascript"; document.body.appendChild(DSLScript); document.body.removeChild(DSLScript); } function callback(oJson) { var tree = "", nbItems; for(sItem in oJson) { tree += sItem + "\n"; nbItems = oJson[sItem].length; for(var i=0; i<nbItems; i++) { tree += "\t" + oSoftwares[sItem][i] + "\n"; } } alert(tree); } function getInfo() { var oParams = { "callback": "callback" }; sendDSL("DynamicScriptLoading_JSON_2.php", oParams); } //--> </script> </head> <body> <p id="main"> <input type="button" value="Récupérer les données" onclick="getInfo();" /> </p> </body> </html> |