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>
Related
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.
There is a javascript on an external webpage that downloads a file. Can I pass a URL to a local script to download this automatically? Simply using http://webpage.com/javascript:theScript() doesn't seem to work, even when I type it into a browser. I'm very much novice in this space, be gentle! Thanks in advance.
This is too little info to tell you exactly how to do it (or even if it can be done at all). But what I can tell you:
http://webpage.com/javascript:theScript() cannot work the way you expect. The URL needs to locate a file, so if webpage.com was hosting a file named exactly `javascript:theScript()', then your browser would download the contents of this file and display it.
If the function you want to call resides in a javascript file, lets say, functions.js (URL: http://webpage.com/functions.js), then you can include this file in your own webpage and call functions from there.
This is where the special javascript: notation comes to place: It's a workaround to allow you to specify a javascript function in a regular <a href...> tag.
File: functions.js
function theFunction() {
alert ("Hey there!");
}
File: yourpage.html
<html>
<head>
<script type="text/javascript" src="http://webpage.com/functions.js">
</script>
</head>
<body onload="theFunction();">
Your text<br>
Click here
</body>
</html>
If this doesn't help you out, then you should post some code (ie. contents of the file containing the function to call, how you want to include the function in your page etc).
To include a script:
<script type="text/javascript" src="http://www.example.com/script.js"></script>
To execute code from the script you'll need a separate script tag:
<script type="text/javascript">
theScript(); //where theScript is a function defined in script.js
</script>
You should look at some tutorials for JavaScript. I recommend w3schools.
What is the correct syntax for adding an external reference to a JavaScript file (.js) within another JavaScript file?
There isn't any.
You can add a <script> block to the document, but it will execute asynchronously.
Alternatively store all your .js files in a php file.
<script src="js.php" type="text/javascript"></script>
php:
<?php
require 'js/myjs1.js';
require 'js/myjs2.js';
?>
I suppose you could load content of file via ajax and use eval().
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.
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.