I'm loading a html template into WKWebView. The HTML has a script tag which refers to a local JS file as mentioned below:
<script type="text/javascript" src="test.js"></script>
But, this js content is not getting loaded into web view at all.
So, i used WKUserScript to inject the content of this file into web view. This resolved my initial problem. But, i'm injecting some additional JS files in 'test.js' at run time. None of these files are getting loaded into web view.
When debugged, i get following error for all these files: 'Failed to load resource: The requested URL was not found on this server.'.
Interestingly, same code works fine when UIWebView is used.
Any input/feedback is appreciated. Thanks.
Yes it can.
load your test.js from Resources bundle
var path = NSBundle.mainBundle().pathForResource("test", ofType: "js")
Convert the file contents to a string, then use the built-in evaluateJavaScript() to load javascript code...
webView.evaluateJavaScript(String.stringWithContentsOfFile(path), completionHandler: nil)
Related
In my static webpage loaded from IIS (v8.5.9600), I include jquery with the following:
<script type="text/javascript" src="js/jquery.min.js"></script>
And when loading the page in the firefox browser I get the following error in my console:
Uncaught SyntaxError: illegal character [Learn More] jquery.min.js:1
If I load it from the CDN, everything is fine, my code runs and I get no errors (my code is in the html page and thus not included from another script).
<script type=“text/javascript” src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js">
I downloaded the file from the CDN and copied it directly into IIS, but when it loads I still get the error in Firefox about an illegal character.
Both of the files from the CDN and IIS load up, so that is not the issue, but I did notice that when the file is loaded from the CDN it looks like this:
But when I load it from IIS (v8.5) it looks like this:
P.S. I tried loading a simple script from IIS (not from the CDN) as well and I get the same error. Is it something to do with IIS's settings?
Check the browser network tab snapshot which shows the script file name and make sure it loads up on its own from the URL you specfied in the src attribute of your <script> tag.
Your Javascript <script> include tags need to have the charset="utf-8" attribute so that the browser requests the script from the server in the utf-8 encoding.
I am getting problem in referencing Javascript file.
My all js files reside in my js folder:MyProject/Js/*.js
Now on my master page i am refering my file like this:
<script src="js/jquery-1.9.1.js"></script>
<script src="js/jquery-1.9.0.min.js" type="text/javascript"></script>
Error showing in console:
SyntaxError: expected expression, got '<'.
On searching i have found that this issue comes due to inappropriate referencing of js files so i have resolve this error by doing this:
<script src="../js/jquery-1.9.1.js"></script>
<script src="../js/jquery-1.9.0.min.js" type="text/javascript"></script>
This above solution works when my .aspx page would be like this:
MyProject/Admin/Abc.aspx //This will work
But problem will occur when any of my .aspx page would be like such:
MyProject/Admin/Subfolder/Abc.aspx // This will not work
My Abc.aspx page contains master page but now in this case this will not load my js files due to ../
So can anybody tell me whats the correct way to reference js files which will work in both the cases??
Note:I am referencing all js files in to my master page and my master page is in:
MyProject/MasterPage.Master
I think you can give a try:
1) Use Bundling to reduce the loading time (Your script will be shorter as well)
2) Use ~/ instead of ../ to make your script/code work even if you relocate the pages.
You can easily find the scripts and codes of jquery bundle if you create a new ASP.NET application in Visual Studio.
Sorry, I don't have a good understanding of the web, but:
When you load in an external script file into an html document, where does it hold or cache that file? It doesn't put it in the index.html file.
<html>
<head>
<script src="name_of_file"></script>
</head>
.....
I ask because I'm working with node.js, and I'm wondering if I list an external script file under my index.html page, I can send the javascript file to the client.
the browser will recognize the "src"="http://xxx/xx.js" of your script tag,and check if the resources(identified with URI:"http://xxx/xx.js") has cached in browser local cache dir(every browser has its own dir)
if the file exist and cache is not expired,the browser will directly load this file,otherwise browser will download the script file,and execute them when download finish.
This question has no good answer. A JavaScript program can be located anywhere on a server, It's just linked to with <script src=SCRIPT></script> Where SCRIPT is the relative or absolute path to the .js file. Check out This site for more info.
It's wherever the file is being served from. With what you've given and default setup, the file will be in the same directory as your index.html file
For some reason my html file's request for my jquery.js file fails, however when I enter that exact url into a new tab in the browser, I get the exact jquery.js file I am trying to get when the website loads.
My code is as such:
<script src="js/jquery.js"></script>
and my website is here. If you append jquery.js to the root, you will see the exact file.
Can anyone explain why the file isn't properly retrieved when the website loads?
Any help is greatly appreciated.
At the beginning of your HTML file is this:
<!DOCTYPE html>
<html manifest="appcache.appcache">
The appcache.appcache refers to the file appcache.appcache whose content is:
CACHE MANIFEST
# V1.65 1-18-2013 10:15 PM
# cache
index.html
about.html
services.html
contact.html
css/signika.css
css/mq.css
css/index.css
css/about.css
css/services.css
css/contact.css
assets/signika_400.woff
assets/signika_600.woff
js/html5.js
js/index.js
js/mq.js
js/services.js
mail.php
images/chicken.png
images/salad.png
images/soup.png
images/steak.png
images/wontons.png
As you can see jQuery.js is not there. I suggest you to add js/jQuery.js into a new line and try again.
I figured this out by:
Running the Fiddler software side by side with Chrome (on Windows).
Loading your website in Chrome with Developer Tools (Ctrl+Shift+I on Windows, Command ⌘+Option ⌥+I on Mac), then go to the Network tab.
Then I see that Chrome loads appcache.appcache from the second time onwards and there's an error when loading jQuery.js.
The manifest attribute refers to the HTML5 cache manifest file.
Tutorial:
http://www.html5rocks.com/en/tutorials/appcache/beginner/
More references:
http://caniuse.com/#search=manifest (browser compatibility)
http://en.wikipedia.org/wiki/Cache_manifest_in_HTML5 (wiki)
https://www.google.com/search?q=html5+manifest
It's probable that the HTML file containing the script header in question is not in the "root" but perhaps in the same JS folder or somewhere else.
So technically this should work:
<script src="/js/jquery.js"></script>
Just keep in mind that this path is relative.
I am trying to load a javascript file stored on the device via html file which is loaded via a webview but never seems to load. I have tried using direct url's like you normally would in html and have also tried:
<script type="text/javascript" src="file:///android_asset/www/js/jsfile.js"/>
JavaScript is enabled on the webview settings too and works fine if I have it on a server.
Thanks if anyone can help.
Hi actually I thing you should call directly the js file because you are calling it from the browser which considers the asset folder being its root folder. You should use the "file:///" prefix when calling from java code. Try something like this:
<script type="text/javascript" src="www/js/jsfile.js"/>
You can use loadDataWithBaseURL.
Put all your javascript under an assets folder and give js file path relative to the assets directory in your script tag (in the html). Don't put a slash in the beginning of src.
Read the html into a string (htmlStr) and then load it in the webview as mentioned below.
webView.loadDataWithBaseURL("file:///android_asset/", htmlStr, "text/html", "UTF-8", null);
It has worked for me.