]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - doc/bugs/javascript_resources_placed_after_html_tag.mdwn
seems like i screwed up javascript loading
[git.ikiwiki.info.git] / doc / bugs / javascript_resources_placed_after_html_tag.mdwn
1 [[!template  id=gitbranch branch=anarcat/js-newline author="[[anarcat]]"]]
3 I am not sure why -- it may be [[my fault|todo/fix_javascript_load_ordering]] -- but it seems like plugins loads there Javascript resources *after* the `<body>` and `<html>` tags are closed. Here's an example from [my sandbox](https://anarc.at/sandbox/):
5     <script src="../ikiwiki/ikiwiki.js" type="text/javascript" charset="utf-8"></script>
6     <script src="../ikiwiki/relativedate.js" type="text/javascript" charset="utf-8"></script>  </body>
7     </html>
8     <script src="/ikiwiki/ikiwiki.js" type="text/javascript" charset="utf-8"></script>
9     <script src="/ikiwiki/toggle.js" type="text/javascript" charset="utf-8"></script>
11 Here, `toggle.js` appaars after the `<html>` tag! Interestingly, this is not specific to that one plugin: the same thing happens, in reverse, in my [recent changes page](https://anarc.at/recentchanges/):
13     <script src="../ikiwiki/ikiwiki.js" type="text/javascript" charset="utf-8"></script>
14     <script src="../ikiwiki/toggle.js" type="text/javascript" charset="utf-8"></script>  </body>
15     </html>
16     <script src="/ikiwiki/ikiwiki.js" type="text/javascript" charset="utf-8"></script>
17     <script src="/ikiwiki/relativedate.js" type="text/javascript" charset="utf-8"></script>
19 I am not sure what determines the order in the end.
21 I'm not sure what's going on, but it seems like the second time a `include_javascript`-like pattern is used, it fails to detect the `</body>` tag and appends the resources at the end instead. That could be because the `</body>` tag is not alone on its own line, which is actually fine in terms of comformity but confuses the regex...
23 I think the simplest solution is to add a newline when we add the javascript blob, to keep the assertion that body is on its own line. I have done so in the aforementioned patch, which is reproduced here for simplicty:
25     diff --git a/IkiWiki/Plugin/recentchangesdiff.pm b/IkiWiki/Plugin/recentchangesdiff.pm
26     index 0093c655e..ac8a61895 100644
27     --- a/IkiWiki/Plugin/recentchangesdiff.pm
28     +++ b/IkiWiki/Plugin/recentchangesdiff.pm
29     @@ -60,7 +60,7 @@ sub pagetemplate (@) {
30      sub format (@) {
31              my %params=@_;
32      
33     -   if (! ($params{content}=~s!^(\s*</body[^>]*>)!include_javascript($params{page}).$1!em)) {
34     +   if (! ($params{content}=~s!^(\s*</body[^>]*>)!include_javascript($params{page})."\n".$1!em)) {
35                 # no <body> tag, probably in preview mode
36                 $params{content}=$params{content}.include_javascript(undef);
37         }
38     diff --git a/IkiWiki/Plugin/relativedate.pm b/IkiWiki/Plugin/relativedate.pm
39     index 083966ad2..1ef6b2e2a 100644
40     --- a/IkiWiki/Plugin/relativedate.pm
41     +++ b/IkiWiki/Plugin/relativedate.pm
42     @@ -26,7 +26,7 @@ sub getsetup () {
43      sub format (@) {
44              my %params=@_;
45      
46     -   if (! ($params{content}=~s!^(\s*</body[^>]*>)!include_javascript($params{page}).$1!em)) {
47     +   if (! ($params{content}=~s!^(\s*</body[^>]*>)!include_javascript($params{page})."\n".$1!em)) {
48                 # no <body> tag, probably in preview mode
49                 $params{content}=$params{content}.include_javascript(undef);
50         }
51     diff --git a/IkiWiki/Plugin/toggle.pm b/IkiWiki/Plugin/toggle.pm
52     index eea6e8861..9f74f6029 100644
53     --- a/IkiWiki/Plugin/toggle.pm
54     +++ b/IkiWiki/Plugin/toggle.pm
55     @@ -68,7 +68,7 @@ sub format (@) {
56      
57         if ($params{content}=~s!(<div class="toggleable(?:-open)?" id="[^"]+">\s*)</div>!$1!g) {
58                 $params{content}=~s/<div class="toggleableend">//g;
59     -           if (! ($params{content}=~s!^(\s*</body[^>]*>)!include_javascript($params{page}).$1!em)) {
60     +           if (! ($params{content}=~s!^(\s*</body[^>]*>)!include_javascript($params{page})."\n".$1!em)) {
61                         # no <body> tag, probably in preview mode
62                         $params{content}=$params{content}.include_javascript(undef);
63                 }
65 Sorry for the trouble, HTH! :)
67 PS: Note that it seems like browsers are happy to ignore the spec and load stuff outside of the `<html>` tag, thankfully. So this is mostly a cosmetic/conformity thing...