I want to include a Javascript function in my Delphi WEBCore application to determine the device screen PPI.
Here is the function:
function getPPI()
{
var div = document.createElement("div");
div.style.width = "1in";
document.body.appendChild(div);
var ppi = document.defaultView.getComputedStyle(div, null).getPropertyValue('width');
document.body.removeChild(div);
return parseFloat(ppi);
}
in the project HTML file I added the following entry:
In the unit, I create a call to the javascript function:
procedure TForm24.GetPPI;
var
PPI: Double;
begin
asm @PPI = getPPI();
end;
ShowMessage('The screen PPI is: ' + FloatToStr(PPI));
end;
In the WebForm create procedure, I then call the function and want to display the value:
procedure TForm24.WebFormCreate(Sender: TObject);
begin
GetPPI;
end;
But I don't see anything at program startup. Where is my error in thinking?
When I comment out the call to the function, the program runs.
procedure TForm1.WebFormCreate(Sender: TObject);
var
PPI: Double;
begin
asm
PPI = getPPI();
end;
ShowMessage('The screen PPI is: ' + FloatToStr(PPI));
end;
You did not specify the script was loaded via a script link.
My guess is that script was not yet fully loaded when you invoke getPPI from the app.
I included the script directly in the main project HTML and it works.
I have now added the function to the project HTML file.
here is the HTML. but still doesn't work.
< !DOCTYPE html>
< html lang="en">
< head>
< meta http-equiv="Content-type" content="text/html; charset=utf-8" />
< meta name="viewport" content="width=device-width, initial-scale=1">
< meta $(ThemeColor)>
< noscript>Your browser does not support JavaScript!
< link rel="icon" href="data:;base64,=">
< meta $(Manifest)>
< link $(FavIcon)/>
< title>TMS Web Project
< script>
function getPPI()
{
var div = document.createElement("div");
div.style.width = "1in";
document.body.appendChild(div);
var ppi = document.defaultView.getComputedStyle(div, null).getPropertyValue('width');
document.body.removeChild(div);
return parseFloat(ppi);
}
< /script>