Webview and external css files. Is it possible?
Webview and external css files. Is it possible?
Dear Vilian,
You have to read it with memoRead() and place it inside your HTML code as explained above
If it is a remote CSS file then you have to download it using FWH WebPageContents( cUrl, lText, nSysWait ) and then StrTran() it into the HTML
#include "fivewin.ch"
static oWebView
function Main()
local oWnd
DEFINE WINDOW oWnd TITLE "Test External CSS"
ACTIVATE WINDOW oWnd MAXIMIZED ;
ON INIT CreaWebView( oWnd ) ;
ON RESIZE if( hb_IsObject( oWebView ), oWebView:SetSize( nWidth, nHeight ), )
return nil
Function CreaWebView( oWnd )
oWebView = TWebView2():New( oWnd )
oWebView:OpenDevToolsWindow(.T.)
oWebView:Navigate( 'file:///d:/fwh/fwhteam/samples/vilian/index1.html' )
Return oWebView<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebView con CSS</title>
<script>
function loadCSS(url) {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = url;
link.type = "text/css";
console.log( "Cargando CSS" );
link.onload = function() {
console.log(`Archivo CSS cargado: ${url}`);
};
link.onerror = function() {
console.error(`Error al cargar el archivo CSS: ${url}`);
};
// Simula un retraso para depuraci贸n
setTimeout(() => {
console.log('Verificando carga...');
}, 2000);
document.head.appendChild(link); // Agrega el CSS al <head>
}
// Uso
loadCSS('file:///d:/fwh/fwhteam/samples/vilian/styles.css');
</script>
</head>
<body>
<h1>Hola desde WebView</h1>
<p>Este texto est谩 estilizado con CSS.</p>
</body>
</html>body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
color: #333;
}
h1 {
color: #ff0000;
}
p {
color: #0000ff;
} TEXT INTO cCss
html, body {
font-family: Tahoma, sans-serif;
background-color: #F5F5EB;
padding: 20px 5px 10px 5px;
margin: 0;
height: 100vh;
overflow: hidden;
}
.container {
display: flex;Obviously
The issue arises when you want to use your own stylesheet because your development is a bit more ambitious, or you "copy" a web page ( or entire site web ) from a project to run it in your webview.
Thank you Guys. I'll try it.