Bonjour à tous,
Je travaille actuellement sur un projet web et j'utilise JavaScript pour pouvoir accéder à des éléments sans recharger toute ma page.
Mon but de charger dans une page html le contenu d'un élément d'une autre page html par exemple:
Ma page1
Code html : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 <!DOCTYPE html> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script> <script type="text/javascript"> jQuery(function($){ $('#result').load('Page2.html #intro'); }); </script> </head> <body> <div id="result"></div> </body> </html>
va charger le contenu de ma page 2:
jusqu’ici tout va bien.
Code html : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 <!DOCTYPE html> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <div id="intro">blablabla</div> </body> </html>
Un problème se pose lorsque je souhaite appeler un élément qui est traité par du javascript dans ma page1
Un exemple avec (dataTable https://datatables.net))
Le résultat final devrait ressembler à ceci:
PageComplete.html:
Code html : Sélectionner tout - Visualiser dans une fenêtre à part
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 <!DOCTYPE html> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="stylesheet" type="text/css" href="http://cdn.datatables.net/1.10.0/css/jquery.dataTables.css"> <script type="text/javascript" charset="utf8" src="http://code.jquery.com/jquery-1.10.2.min.js"></script> <script type="text/javascript" charset="utf8" src="http://cdn.datatables.net/1.10.0/js/jquery.dataTables.js"></script> </head> <body> <table id="example" class="display" cellspacing="0" width="100%"></table> </body> <script> //contenu de ma dataTable var dataSet = [ ['Trident','Internet Explorer 4.0','Win 95+','4','X'], ['Trident','Internet Explorer 5.0','Win 95+','5','C'], ['Trident','Internet Explorer 5.5','Win 95+','5.5','A'], ['Trident','Internet Explorer 6','Win 98+','6','A'], ['Trident','Internet Explorer 7','Win XP SP2+','7','A'], ['Trident','AOL browser (AOL desktop)','Win XP','6','A'], ]; $(document).ready(function() { $('#example').dataTable( { "data": dataSet, "columns": [ { "title": "Engine" }, { "title": "Browser" }, { "title": "Platform" }, { "title": "Version", "class": "center" }, { "title": "Grade", "class": "center" } ] } ); } ); </script> </html>
Maintenant j’essaie d'appeler dynamiquement
Code html : Sélectionner tout - Visualiser dans une fenêtre à part <table id="example" class="display" cellspacing="0" width="100%"></table>
de la Page2 vers la Page1 comme ceci:
Page1.html:
Code html : Sélectionner tout - Visualiser dans une fenêtre à part
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
53
54
55
56
57 <!DOCTYPE html> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="stylesheet" type="text/css" href="http://cdn.datatables.net/1.10.0/css/jquery.dataTables.css"> <script type="text/javascript" charset="utf8" src="http://code.jquery.com/jquery-1.10.2.min.js"></script> <script type="text/javascript" charset="utf8" src="http://cdn.datatables.net/1.10.0/js/jquery.dataTables.js"></script> </head> <body> <input type="button" onClick="loadData()" value="load"> <div id="contenuPage2"></div> </body> <script> function loadData(){ $('#contenuPage2').load('Page2.html #example'); } //contenu de ma dataTable var dataSet = [ ['Trident','Internet Explorer 4.0','Win 95+','4','X'], ['Trident','Internet Explorer 5.0','Win 95+','5','C'], ['Trident','Internet Explorer 5.5','Win 95+','5.5','A'], ['Trident','Internet Explorer 6','Win 98+','6','A'], ['Trident','Internet Explorer 7','Win XP SP2+','7','A'], ['Trident','AOL browser (AOL desktop)','Win XP','6','A'], ]; $(document).ready(function() { $('#example').dataTable( { "data": dataSet, "columns": [ { "title": "Engine" }, { "title": "Browser" }, { "title": "Platform" }, { "title": "Version", "class": "center" }, { "title": "Grade", "class": "center" } ] } ); } ); </script> </html>
Page2.html:
Code html : Sélectionner tout - Visualiser dans une fenêtre à part <table id="example" class="display" cellspacing="0" width="100%"></table>
Ma Datatable ne s'affiche pas lorsque je clique sur mon bouton "load"
Comment puis je faire pour avoir le même résultat que PageComplete.html ?
(j’espère avoir été assez clair)
En vous remerciant par avance.
Partager