How to correctly include a javascript file into html - javascript

I am trying to run a script through HTML but I am having some problems. I searched online and found how to do so, however the issue is that even if I correctly type the path of the .js file, it seems to add some strange characters before it.
This is in index.html
<script type="text/javascript" src="fractalTest/fractalTest.js"></script>
I expected this to work but when I open index.html in google chrome and inspect then look under the elements tab, this "â©fractalTest/fractalTest.js" is replacing "fractalTest/fractalTest.js" in the path of the file. I believe this is whats causing the error but I do not know how to fix it!

...it seems to add some strange characters before it.
That usually means that the file is saved with a byte-order mark (BOM) of some kind, but isn't being sent with the correct charset for that byte-order mark.
Be sure that the server is configured to serve the files with a specific encoding (UTF-8 is a good choice), and that you save the files using that encoding (in your text editor, etc.). It's also usually best not to include a BOM on UTF-8 files (although it's valid, some tools don't handle it well).
Side note: No need for the type attribute. The default is JavaScript.

Related

Unable to load a file with ajax load method

$(function(){
$("#testdiv").load("sometext.txt",function(){
alert("Succes");
});
});
I get the alert but I am not able to see anything in the "#testdiv".The file,sometext.txt is in the same folder.I tried giving it absolute path but i get 404 ,url not found on this server.
What am I doing wrong here?
Windows has several "features" that are built to make their operating system more usable for most people. Hiding the file extension is one of those "features" that often do more harm than how much they help.
Though, as you might know, a file doesn't need a file extension, the first few bytes of the file are used to tell the OS what file type it is, the extension just makes it easy.
When using windows to develop anything, you probably want to see the actual name of your files, so make sure to check out one of the guides on how to enable displaying the file extension, like this one.
If you can't GET a file with JS always double, or better even tripple check, that the file can be accessed through the browser first.

MathJax in MediaWiki: escaped syntax is shown, console has 404 errors

I've installed mediawiki-1.26.2 on a private server and want to display latex formulas in that. So I decide to use Extension:MathJax.
At first I installed a local copy of MathJax to render the equations (at top level of server:/var/www/html/mediawiki-1.26.2/). After completing the installation I tested it by visiting http://(Server)/MathJax/test, where the page tells me that "MathJax Appears to be Working!".
Then I downloaded the extension, extracted archive, renamed it to "MathJax" and moved it to “extensions” sub directory of mediawiki-1.26.2. After that I created symbolic link to "mwMathJaxConfig.js" in [local MathJax location]/config/local which is "/var/www/html/mediawiki-1.26.2/MathJax/config/local" directory. So already there exists two files:
local.js
mwMathJaxConfig.js (symbolic link)
Then I changed the last line of "mwMathJaxConfig.js" from:
MathJax.Ajax.loadComplete("_SUBSTITUTE YOUR URL___/mathjax/config/local/mwMathJaxConfig.js");
to this:
MathJax.Ajax.loadComplete("http://localhost/MathJax/config/local/mwMathJaxConfig.js");
Because extension page says: "You might need to modify mwMathJaxConfig.js file to make this work ... ".
After all of them, I tested did extension installed correctly? (wiki → special pages → version → Installed extensions part) And name of "MathJax" was there.
But when I try to show a latex in mediawiki, even simple terms like these:
We consider, for various values of $s$, the $n$-dimensional integral
:<math> \frac{f}{g} </math>
do not shown correctly. Results are:
We consider, for various values of $s$, the $n$-dimensional integral
\[ \frac{f}{g} \]
You can see :<math> is replaced by \[ and </math> by \]. So I guess MathJax is loaded but does not load correctly and problem is in about last line of mwMathJaxConfig.js.
Also I saved page as html and looked at to its code and only this part of code was related to MathJax:
<script type="text/javascript" src="./PageName_files/MathJax.js"> </script>
Above code was not in head and PageName is the name of saved page. I think that is replaced instead of this code which is described in MathJax docs:
<script type="text/javascript" async src="path-to-MathJax/MathJax.js?config=TeX-MML-AM_CHTML"></script>
I looked at Third-party Extensions in MathJax docs but did not understand its meaning.
Also I checked local.js file in [local MathJax location]/config/local whose last line is:
MathJax.Ajax.loadComplete("[MathJax]/config/local/local.js");
Do I have to change the last line of that and replace another string instead of [MathJax]?
The solution which worked for Davide Cervone and hasanghaforian:
Name of directory of local MathJax must be mathjax and not MathJax, then \frac{f}{g} is displayed correctly.
Two errors are shown in the browser console:
MathJax.js?config=TeX-AMS-MML_HTMLorMML-full,local/mwMathJax‌​Config:19 GET http://localhost/mathjax/extensions/TeX/xypic.js?V=2.7.0
MathJax.js?config=TeX-AMS-MML_HTMLorMML-full,local/mwMathJax‌​Config:19 GET http://localhost/mathjax/extensions/fp.js?V=2.7.0 404 (Not Found).
These two files are part of a third-party extension called XyJax for handling commutative diagrams called xypic. I suspect that the MediaWiki extension you installed is configured to load that, you would would need to obtain it separately from the link above. I don't know a lot about the extension, but I think that the references are to an old version, and that both xypic.js and fp.js have been combined in later versions. If you don't want to do commutative diagrams, you can remove them from the mwMathJaxConfig configuration file.

JavaScript critical error

I am unable to include any javascript file in either the _Layout.cshtml or any other view. When I do so, I get the error "javascript critical error at line 3...". I'm using IE to browse. Errors are not shown in Chrome though. I am including the js file at the bottom of the view like this:
<script src="~/Content/bootstrap/js/bootstrap.min.js"></script>
The error is not specific to this file, any js file gives the error. I also tried sing the url.content, no luck. Thanks
To me it seems quite unrelated to ASP.NET since you are including a static JS file, so it's pure HTML.
I suppose in this case you should remove the tilda character ~ from file URL.

Usage of charset in the script tag for Javascript

I need to include text of different languages in the javascript app that I'm writing, and the characters need to show up properly in different webpages that have different page encodings. The script is external.
I've read that in order to have the browser correctly interpret and display these characters, a charset tag should be added in the script tag. However, I am facing a few problems with this approach and would like to see if anyone could help.
The order of the charset seems to matter in IE9. Should "charset" be declared before "src" in the script tag? Is there such a specification or did anyone see a similar issue?
I've seen someone comment in another forum that the charset in an element will not be processed if the webpage defines a charset in the http content type/meta (or something along that line). Is this true? Does this mean that the charset in the script tag will be ignored then? If so, how to overcom
Are there any other recommended methods besides using charset in the script tag? Manipulating the webpages is not an option.
If I understood you correctly, you have multiple js files each one using a different encoding, and you tried the charset on the <script> tag but that is causing you troubles.
If so, have you tried not adding the charset at all and having the javascript files encoded in Unicode? I found that to be the best in having the browser actually render the correct fonts.
read this for some examples and let us know if it works out for you!

JavaScript Character Issue When Filling Dropdown With jQuery From External JS File

I'm running into a character encoding issue when I load a dropdown using jQuery from an external js file. This only seems to happen when the JavaScript object is not within the page.
For example the below is the JavaScript object.
var langs = [
{value:'zh-CN', text:'中文 (简体) Chinese Simplified'},
{value:'en', text:'English'},
{value:'eo', text:'EsperAnt'},
{value:'es', text:'Español'},
{value:'ja', text:'日本語 (Japanese)'},
{value:'pt-PT', text:'Português'},
{value:'ru', text:'Русский (Russian)'},
];
If this is in my page with the proper meta tags <meta http-equiv="content-type" content="text/html; charset=utf-8" /> the below code works.
$(document).ready(function() {
// Fill language select
$.each(langs, function(i, j){
$('#LangSelect').append($("<option></option>").attr("value",j.value).text(j.text));
});
But, since I need languages on more then one page I've moved the langs object to an external js file and reference it. After doing this, I run into encoding issues such as russian characters become РуÑÑкий (Russian).
This encoding issues seems to still appear even when the reference to the external js file is set as below:
<script type="text/javascript" charset="UTF-8" src="externalJS.js"></script>
Is there anyway to force the JavaScript object to be loaded with the proper encoding from an external file?
Please note I am experiencing these issues when viewing content on the iPhone Mobile Safari browser. Additionally these pages are simply html and JavaScript without any server side components.
Thanks in advance,
Ben
Is there anyway to force the JavaScript object to be loaded with the proper encoding from an external file?
Yes, the script charset attribute as you quoted. However it historically didn't work everywhere and was best not relied on. Where this is not supported, the browser will always use the charset of the main page as the charset in the script. So as long as you include the UTF-8 charset parameter in the main page you should be fine either way.
I am surprised if a modern browser like Mobile Safari doesn't understand it, though.
Is it possible your server might be serving .js files with a bad Content-Type header containing a wrong charset? A combination of unset mime-types for JS plus AddDefaultCharset in Apache could leave you with:
Content-Type: text/plain;charset=iso-8859-1
Which might maybe have the effect of mucking it up.
Make sure you save the javascript file using UTF-8 encoding. If you open the file in Notepad++, then you can click Format>Encode in UTF-8 (If you try Format>Convert to UTF-8, then have a look at the page using a hex editor. Sometimes you end up with some strange characters at the beginning of the file).

Categories