Javascript/jquery not working on my site - javascript

I'm creating a site, tested out on my local server and it worked fine, but once I moved it over ot my host anything to do with javascript/jquery on the page just refuses to work :/
http://www.neuromanga.com/mangaReader1.0.php?cid=1
could someone please help me out

You have <script> tags in your Javascript file. They need to be removed. I think this will fix your problem.

Please remove the <Script> and </Script> tags from your Mangacaller.js file.
if you use <Script src='...'/> you don't use the script tags in that source file.

The external script (Mangacaller.js) should not have the <script type="text/javascript"> and the corresponding </script> inside the external file. The external file should be all JavaScript without any HTML (it's just the stuff that would go between the <script> tags if you embedded the script inside the HTML instead of referencing it as an external script). The browser parses this entire external file as JavaScript, not HTML, so there should be no HTML within the Mangacaller.js file.

Related

Migrate non function script from HTML to JS file

As a begineer i am facing a problem, i followed an advise from several opinions to keep my HTML file clean, so i created a JS file and started migrating all scripts to a it, everything is Ok until i had to make this in it :
<script>
document.getElementById("mobile-toggle").addEventListener("click",function(){
document.getElementById("mobile-date").innerHTML = "Today is ...";});
</script>
When this script in is my HTML file it is run by the browser automatically but when i put it in the JS file, it simply don't work without being called with a function name, and that's what i want to avoid due to unobtrusive javascript recommendations, so my question is "is there a way to make a script from JS file to be run automatically without a call from HTML event ?
Thanks.
Include your script in the HTML with defer attribute. This will run the code when HTML is ready. You could place the script tag at the end before </body> as well, but I prefer having it on top.
<head>
<script src="/myscript.js" defer>
</head>
in myscript.js
(function() {
// your code goes here
})();
Wrapping this into a function gives your own scope and another wrap into parenthesis and () at the end will do the execution.

Should I leave the script tag empty if I use an external script file?

I mean, if I use JavaScript in an external file(write script code in another file, not in HTML file).
I write this code in my HTML file <script src="filename.js"></script>
But this code works only if I left the tags empty.
So I'm wondering that if we use JavaScript in an external way, should I leave the tag empty as I mentioned?
Yes.
It's important to note that a script element using the src attribute should not include additional Javascript code between the <script> and </script> tags. If both are provided, the script file is downloaded and executed while the inline code is ignored.
Reference
Note that html5 allows comments inside an external script tag.
Reference html5

Only last javascript file linked with script tag is working

I linked all the javascript files in the header.php at atime. I included header.php in all pages
When I link the javascript files like this
<script src='js/home.js'></script>
<script src='js/disc.js'></script>
<script src='js/que.js'></script>
only last file js/que.js is working.
Make sure that the 'src' is referring to the correct file directory where the script is located. If you are using an IDE such as VS, then you may drag the file into the code and the IDE will automatically create the reference for you.
Unless you have problem with directory structure or you file name does not match with src attribute of script tag, there should not be any problem with. Please try to use type="text/javascript" and make sure your script tags are after header tag or just before </body> tag. Also keep sequence of files loading if any file depends on another file variable or any function. If it still does not work use try to see if there is any error in your code in console window.
I now I'm going to get voted down for this but oh well. I don't have enough points to comment which is what I would do but..oh well.
Okay it depends on what the scripts are doing. If you link the scripts in the head for you HTML page, and try a var element = document.getElementById("theID"); this will return null due to the fact that the browser has yet to read the HTML and hasn't had a chance to create a DOM (Document Object Model) tree. For a problem like this check out
<script>
function load() {
console.log("load event detected!");
}
window.onload = load;
</script>
This will assign window to an event/callback that will be invoked after the page has had time to load.
Look at where your JavaScript is used, and what it should be doing. Would those elements be rendered yet. Are you writing functions but not actually calling them (this happens A LOT)? It would be better if you described what the code was doing and your experience with HTML and JavaScript. Remember that the browser interprets the JavaScript as it encounters it, you can put script tags anywhere in your HTML file, not just in the head. Also are the .js files in the same directory as the HTML file or are the two non working .js files in the same directory as "js/que.js"? If not move them to the same file or use a relative or absolute path.

Should external javascript file start and end with <script> </script>?

I want to use jquery in my web application. Should I include it as a link in my external JavaScript file or inside my html file?
Thanks in advance
No. The way to include a javascript file is using <script src="file.js"></script>
The contents of file.js should not be surrounded with <script> tags as that is html code.
Yea sorry i was mistaken, didn't think external javascript counts but apperantly it does.
<script src="myScript.js"> code in here </script>

Can a single javascript file be used by multiple html files?

I have a javascript file main.js and five html files 1.html,2.html,3.html,4.html,5.html
I want to access the javascript file main.as in all files .
I have used in all of the five but I'm not able to access it . Is it possible to share the .js file among the html files.
Plz reply,
Thanks in advance
Prashant Dubey
Yes, it is entirely possible. That's the point of being able to have a JS file instead of having to embed all the code in the HTML.
yes this is well possible. You merely have to include
<script type="text/javascript" src="main.js"></script>
in your HTML files, preferably near the bottom (for faster rendering). What have you done so far?
Yes. Totally possible.
Just reference it in all of the files e.g. by
<script type="text/javascript" src="Scripts/main.js"></script>
Yes, it is possible.
Probably there is something wrong with the way you access the javascript from your html. Show us the <script ...>...</script> part of your html.
Yes. Are you using the correct path to the main.js file in your html files?
Create separate javascript file with .js extension with all your function in it and
just include this javascript file in the head tag of all the html scripts u wanna use that in.
Like::
<html>
<head>
<script type="text/javascript" src="JavaScriptFilePath.js"></script>
</head>
<body>
<!-- use javascript -->
It can happen both ways..
a single html file can use multiple javascript file
2.a javascript file can be used in several html files.
In first case javascript file load can be conditional based on location, user preferences, time, age group, content restriction.
You can see good example when facebook loads its page. I loads number of javascritps.

Categories