Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load and import a CSS/JS file from the phone in phonegap?

I use a javascript code to download a CSS file to the phone.

The code works well and it saves the CSS file in this path:

file:///data/user/0/com.my.app/files/file.css

Now I want to locally load it with a html code (in my server) like this:

<link rel="stylesheet" href="file:///data/user/0/com.my.app/files/file.css">

but it doesn't work.

I also tried to put the CSS file directly into the package (.apk and .ipa) in a css folder in the www folder, but this code doesn't work too:

<link rel="stylesheet" href="file:///android_asset/www/css/file.css">

Is there a way to load a CSS/JS file locally from the phone?

EDIT:

My phonegap application has a index.html file in the www directory that simply redirect to my server (ie. http://www.myserver.com). What I want to do is importing CSS or JS files in my server and take them directly from the phone.

Maybe can I use a iframe (in the index.html file) and load there all the code in my server instead of using a redirect?

like image 849
VanDir Avatar asked Jul 19 '16 18:07

VanDir


People also ask

How do I import a CSS file?

Note: There are two different ways to import a CSS file into another using @import url(“style2. css”); or @import “style2. css”; or directly import any CSS file or multiple CSS file in the HTML file directly within <style>@import “style1.

How do I run HTML CSS and JavaScript files together?

To link a CSS file with your HTML file, you have to write the next script on your HTML file inside the head tag. To link a Js file with your HTML, you only have to add the source of the script inside the body tag or outside; it doesn't matter.

How do I include external JavaScript and CSS file in HTML?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.


1 Answers

In your second attempt, you placed the file in the css subfolder of the www root folder. That's the right way to do it, but then you need reference it using a relative path like this:

<link rel="stylesheet" type="text/css" href="css/file.css" />

Edit

First of all, if your app is redirecting to the server, Apple and maybe other stores will reject your app. According to Apple's guidelines, your app must have functionality beyond being just a "repackaged website".

Having said that, since you're redirecting to the server, so your CSS (and JS) is best left on the server and not imported locally. Just make sure your webpage works properly in browser and links to the CSS file on the server in the standard way, and it will work properly when your app redirects to it.

Of course, you need to whitelist your website, but I assume that you've already done that.

like image 102
Racil Hilan Avatar answered Oct 04 '22 01:10

Racil Hilan