I need my java.js file to reference other .js files - javascript

Initially my HTML file contained all javascript but I've tried to push it into it's own seperate java.js file and then reference it from the HTML. The current java.js requires the code from "jquery-csv.js" and "jquery-1.8.2.js" but since I've moved the javascript into it's own file, it no longer works. I assume this is because the java.js no longer has access to the other .js files.
<script src="jquery-1.8.2.js"></script>
<script src="jquery-csv.js"></script>
<Script src="Java.js"></script>
How would I be able to make it so the java.js file can reference and call the other .js files to ensure the code works?

Related

Difference between linking a javascript file and directly adding script into html

I am recreating a javascript game which requires tables, but when I link my javascript file to my index.html the grid does not show on my browser, but when I directly add my javascript inside the script tag in my html, the grid shows on the browser.
Any help would be appreciated thank you!
This is my code by referencing a js file inside html(the tables do not show)
html with js link
This is my code without referencing a js file(tables do show)
html without js link
This is my main.js
main js
This is the browser without linking js file
browser without js file
This is my browser with linking js file
browser with js reference
First I linked the js file with script src=js path but it did not work but it worked when I put the javascript directly inside the html script tag. I was wondering how I can make it work with referencing a separate js file for a cleaner html code.
It's due to that your local paths are not resolving correctly. Without seeing directory structure I can't know what could be wrong.
First of all, please re-check and verify that both files main.js and index.html are in the same directory.
Alternatively, you could create folder called js inside of your main directory (where index.html is) and then move the main.js into that folder and try:
<script type="text/javascript" src="/js/main.js"></script>
index.html and main.js files must be at the same directory in order to include .js file as you did
<script type="text/javascript" src="main.js"></script>
UPDATE (I will leave answer whole, if somebody also gets stuck, so they can troubleshoot the problem):
The reason why it couldn't resolve path is because there was blank spaces between src and = ; when giving src, that part needs to be connected.
Just like this
<script type="text/javascript" src="main.js"></script>
Take a look here:
What happens:

How to make sure JS file runs in external php without including again?

I have a PHP script that includes a .JS file. The PHP file makes a jquery call to an external functions.php file which requires the same .JS file, however, with it being a separate file, I have to include it again inside functions.php. If I do this, it breaks the first include, but the 2nd include works. If I remove the include from the external file, the script doesn't work, but everything in the original file does.
Is there a way to include a JS file in the external file without calling it again? Can it just call from the first include?
Included as:
<script type="text/javascript" src="js/scripts.min.js"></script>
Using php's include_once
should resolve your problem. This function ensures that the given file will only be included once.

script.js file not linked to html?

I'm trying to replicate this jsfiddle project on my local machine
I have copied the exactly displayed contents of the index.html, script.js, and style.css files on my local machine. Although my local files didn't initially work the desired way (displaying song length, title, etc).
So I added these lines to the top of the html file, so that my script.js file would be linked. I also added a line to my html file so that my downloaded jquery file would be recognized. With these lines added to my html file:
<script src="jquery-3.3.1.js"></script>
<script type="text/javascript" src="script.js"></script>
But when loading my index.html file into my browser, I can choose a file just fine, but none of the javascript content is displayed. I don't think my script.js file is being recognized, because even If I add console.log() lines to the script.js file, they aren't displayed.
How do I ensure my script.js file is correctly linked so that this jsfiddle project works on my local machine the same way it works on jsfiddle?
thank you
You can use cdn
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Or if you download the file you have to be sure where is this file in your project
<script src="project/jquery.min.js"></script>
You can try this.
You need to make sure you have jquery downloaded locally or use a CDN in your html file. See below.
Also, make sure you have your script.js in the same folder of your index.html or point to the file correctly.
<script src="https://rawgit.com/moment/moment/2.2.1/min/moment.min.js">
</script>
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous">
</script>
<script type="text/javascript" src="script.js" defer></script>

JavaScript use script from other JavaScript file

I have two JavaScript files in a web application project and want a function in one file to call a function from the other. How can that be done?
Refer to both files from the HTML page
<script src="js/file1.js"></script>
<script src="js/file2.js"></script>
functions in one file should be accessible from functions in another file.
When both JS files are parsed by a browser functions from one file will be visible to another and vice versa.
Please bear in mind that the JavaScript file which should be used in another JavaScript file usually needs to be declared before the other JavaScript file (using the HTML <script> tag).

Load JavaScript in Google App Engine

I got so confused loading JavaScript in Google App Engine. I am using the Django template.
First, in my base HTML file, I can't load my downloaded jQuery code from local say, d:/jquery.js, like
<script src="d:\jquery.js" type="text/javascript" ></script></head>,
This line is in my base HTML file. It works when I load jQuery from remote. Like
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"type="text/javascript" ></script></head>
I don't know why.
Second, I can't load my own-created JavaScript code to my HTML file. Say I create a JavaScript file, like layout.js, and I try to load it like this in my child HTML file, which, by the way, inherits from the base HTML.
<body><script src="layout.js" type="text/javascript"></script></body>
And it doesn't work at all. The only way it works I have tried is when I put the actual JavaScript code in the body of my base HTML file. Like
<body><script>
$(document).ready(
$("#yes").click(function() {
$("#no").hide("slow");
}));
</script>
I don't know why either... How do I fix it?
AppEngine doesn't know anything about paths on your local system; it will only upload files that you configure it to. Do this by having a line like this in your app.yaml file:
handlers:
- url: /js
static_dir: js
In this case, /js represents a subdirectory of your main project directory, and you can put all of your static JavaScript files in there. They will be uploaded to the production server, and you can include them in your HTML with:
<script src="/js/jquery.js" type="text/javascript" ></script>

Categories