fpweb and pas2js lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- krolikbest · 2021-04-21 17:06 Hi, let's say this simple code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program project1; {$mode objfpc}{$H+} uses   {$IFDEF UNIX}{$IFDEF UseCThreads}  cthreads,  {$ENDIF}{$ENDIF}  sysutils, fphttpapp, HTTPDefs, fpwebfile, httproute, fpWeb; var  someVar, buff: String; procedure Test(aReq: TRequest; aRes: TResponse);begin  someVar := aReq.QueryFields.Values['txt']; //get method  writeln(someVar);  if somevar='12' then  begin    ARes.ContentType := 'text/html; charset=utf-8';    ARes.Contents.LoadFromFile(ExtractFilePath(ParamStr(0)) + 'project11.html'); //????????  end    else  begin    ss:='HOME';    aRes.Code:=200;    aRes.ContentType := 'text/html;charset=utf-8';    aRes.Content := ''+buff+ss+'';    aRes.ContentLength := Length(aRes.Content);    aRes.SendContent;    buff:=somevar;  end;end; begin  Application.Port:=8080;  HTTPRouter.RegisterRoute('/',@Test, true);  Application.threaded := true;  Application.Initialize;  Application.Run;end.In the web browser:  http://192.168.1.4:8080/?txt=12 runs ok if someVar<>'12'. If someVar='12' then it should load project11.html (this is a file made in pas2js). But evidently doesn't. In Chrome Console I see "Uncaught SyntaxError: Unexpected token '<' ?txt=12:13 Uncaught ReferenceError: rtl is not defined at ?txt=12:13" If file project11.html doesn't contain javascript then it loads. Hints? Regards, ---------------------------------------------------------------------- ojz0r · 2021-04-21 17:52 I had problems with external files in fpweb, what i did is to add: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i RegisterFileLocation('your.file', ExtractFilePath(ParamStr(0)));  Before: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i HTTPRouter.RegisterRoute('/',@Test, true);  Perhaps its the same issue? ---------------------------------------------------------------------- krolikbest · 2021-04-21 18:05 If you work in Linux there it won't be probably  issue with RegisterFileLocation, but I work presently in Win7 (Laz 2.0.10) and have issue with this routine. I had it earlier as well but can't recall where I read about this issue with  RegisterFileLocation.. ---------------------------------------------------------------------- PierceNg · 2021-04-22 04:41 Quote from: krolikbest on April 21, 2021, 07:06:39 pmIn the web browser:  http://192.168.1.4:8080/?txt=12 runs ok if someVar<>'12'. If someVar='12' then it should load project11.html (this is a file made in pas2js). But evidently doesn't. In Chrome Console I see "Uncaught SyntaxError: Unexpected token '<' ?txt=12:13 Uncaught ReferenceError: rtl is not defined at ?txt=12:13" If file project11.html doesn't contain javascript then it loads. Hints? What does project11.html look like? Is it that project11.html does not run the pas2js RTL? Code: Javascript  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ---------------------------------------------------------------------- krolikbest · 2021-04-22 07:34 If independently then Project11.html starts in the browser. But doesn't if I try to start it as described earlier. ---------------------------------------------------------------------- egsuh · 2021-04-22 07:51 There could be some problem with Project11.html. Check the encoding type of project11.html file, and whether no template replacement is working. Display the source of erroneous result on browser, and compare it with the original file (opened in text editor). If you see any difference, problem lies there. ---------------------------------------------------------------------- PierceNg · 2021-04-22 08:02 Quote from: krolikbest on April 22, 2021, 09:34:50 amIf independently then Project11.html starts in the browser. But doesn't if I try to start it as described earlier. project11.html and its assets like pas2js-transpiled Javascript are separate files. Could it be, when you load 'independently' whether from disk or via a web server, all assets are served, but your fpWeb application is however serving only project11.html and not the Javascript etc it references? ---------------------------------------------------------------------- krolikbest · 2021-04-22 08:21 In attachment project11.html, project11.js, I made just now in order to check on different machine and place ---------------------------------------------------------------------- PierceNg · 2021-04-22 09:34 Quote from: krolikbest on April 22, 2021, 10:21:33 amIn attachment project11.html, project11.js, I made just now in order to check on different machine and place Funny error. I am on Linux. I added a RegisterFileLocation statement to main program and the program works as expected: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i begin  Application.Port:=8080;  RegisterFileLocation('project11.js', ExtractFilePath(ParamStr(0)));  HTTPRouter.RegisterRoute('/',@Test, true);  Application.threaded := true;  Application.Initialize;  Application.Run;end. ---------------------------------------------------------------------- krolikbest · 2021-04-22 09:47 Yes, I know. Unfortunatelly i'm on Win7... That why I have some issue with RegisterFileLocation. ---------------------------------------------------------------------- PascalDragon · 2021-04-22 11:28 Quote from: krolikbest on April 22, 2021, 11:47:47 amYes, I know. Unfortunatelly i'm on Win7... That why I have some issue with RegisterFileLocation. You can either use FPC 3.2.2 RC1 which has that problem fixed or as a workaround you can pass a so called drive absolute path: if your relative path is .\files, your absolute pah is C:\projects\webtest\files then the drive absolute path is \projects\webtest\files assuming that your current directory is a folder on drive C:. You then need to pass /projects\webtest\files (note the first / instead of \) to RegisterFileLocation so that it doesn't adjust the path. To do this in code you need to use ExpandFileName, remove the first two characters (if they're an ASCII letter and a “:”) and change the \ at the now first position from a \ to a /. ---------------------------------------------------------------------- krolikbest · 2021-04-22 12:06 Thanks. As soon as i'm on this prioject will do as you said.