react-native icon indicating copy to clipboard operation
react-native copied to clipboard

[WebView] Is it possible to load local js file in WebView html prop?

Open bright-sea opened this issue 11 years ago • 26 comments

For Example, I put a web/jquery.js file in bundle, then I would like to use:

var html=`
<html>
<script src="web/jquery.js"></script>
...`;

then use WebView to set html prop to html.

I tried to set src to jquery in CDN, it works fine. but don't work with above code.

Is it because currently local js in WebView not be supported? Or I miss something at here?

bright-sea avatar May 29 '15 07:05 bright-sea

I'm not sure you can do this, but if you can you would use a file:// protocol, which you would grab from the following snippet (I'm assuming jquery.js is in your xcode project)

NSURL *jqueryLocation = [[NSBundle mainBundle] urlForResource:@"jquery" withExtension:@"js"];

That would return a uri with something like file://something/Devices/abcd-123/caches/bundle/jquery.js which is the absolute path on the device/emulator after compiling your app

JohnyDays avatar May 29 '15 08:05 JohnyDays

@bright-sea - I think what we want here is something like:

<WebView url="/web/index.html" />

Then we just check if the url is http http or not - if not, we do something like this

Or we could explicitly support setting a root

<WebView baseURL="/web" html={html} />

And then you can pass in your html prop just like above.

Do you want to try submitting a patch for that? cc @JohnyDays

brentvatne avatar May 29 '15 17:05 brentvatne

@brentvatne I don't have access to a MAC over the weekend, if noone takes it up until monday I'll give it a shot. However I don't think that's what @bright-sea was asking for! His question was more about requiring a <script> tag inside of the html(which is inlined and dynamically generated), where the source script was a file required inside the app bundle. Though perhaps you just explained how to do that and I misunderstood.

JohnyDays avatar May 29 '15 19:05 JohnyDays

@brentvatne - set url or html to a local html file isn't what I want. What I need is to put some js files( orcss files or any other web assets) into bundle, then in html template we can use these files so that we can do any kinds of works from there.

@JohnyDays - Thanks, your suggestion helps me. I use react-native-fs to get the path of main bundle, then use list below code and it works fine:

<script src="file://${mainBundlePath}web/jquery.js"></script>

Of couse, if we can have this kinds of support in react native directly instead of depends on third party module would be great.

bright-sea avatar May 29 '15 19:05 bright-sea

@bright-sea - the second option I proposed would help:

<WebView baseURL="/web" html={html} />

By setting the baseURL, like in the example I linked to on StackOverflow, the WebView knows where to look for the css/js that you link to :smile:

The html var I'm using there is just the same as what you used in your original post.

brentvatne avatar May 29 '15 19:05 brentvatne

@JohnyDays - that would be great! looking forward to seeing it when you have time.

Given that this issue has been solved for now I'll close this, but I hope to see a PR to add something like what I mentioned above to WebViews - I think that would be very useful.

brentvatne avatar May 29 '15 19:05 brentvatne

@brentvatne yes, you are right! Your proposal will be more elegant.

bright-sea avatar May 29 '15 19:05 bright-sea

Oops apparently having a <script> tag in your comment hides the rest, apologies!

JohnyDays avatar May 29 '15 19:05 JohnyDays

@bright-sea Hi. Your solution works like a chart. But would that work even App is packaged and deployed on a device ? Thanks You..

gs-akhan avatar Nov 13 '15 15:11 gs-akhan

If I package my app and view it on an iOS device, the asset files are not there. The only file, that is there, is the html file used by the WebView (and required). How can I make sure, the packager copies the *.js and *.css files to the asset directory?

richardbrammer avatar Mar 05 '16 16:03 richardbrammer

I have the same question as @richardbrammer. Any updates or guidance would be very welcome.

DanielApt avatar Sep 26 '16 11:09 DanielApt

+1 on Android @richardbrammer .

qxqxqxqx avatar Oct 21 '16 05:10 qxqxqxqx

+1 on Android

ghost avatar Nov 13 '16 12:11 ghost

+1 on Ios

matdru avatar Apr 12 '17 07:04 matdru

+1 on android

VishwajeetDagar avatar Apr 14 '17 08:04 VishwajeetDagar

what is will like to ask is how to load a react base webapp on a webview. i think that is what he is asking ..

AyubaMakoa avatar Sep 18 '17 09:09 AyubaMakoa

How this can be closed, when people still are having issues with loading local html, css, js files, crossplatform in react-native :( @brentvatne

brancooo1 avatar Oct 09 '17 20:10 brancooo1

@brancooo1 - this isn't the right place for feature requests, if this hasn't been implemented it's because the contributors to the project don't need it for their apps. you can post a feature request to https://react-native.canny.io/feature-requests or implement it yourself or pay someone to implement it for you :)

brentvatne avatar Oct 09 '17 20:10 brentvatne

I am also facing the same issue. My .js .css files are loaded on simulator and it is working as expected. But those files are not loaded on iOS device. any help?

sujitpk-perennial avatar Oct 10 '17 03:10 sujitpk-perennial

After doing some R&D, for me expo was creating the issue. After uninstalling the expo from the app, app is working as expected. It looks like expo is ignoring the js, css files.

sujitpk-perennial avatar Oct 10 '17 14:10 sujitpk-perennial