So I found online javascript tutorials and I downloaded notepad++ to write my code. The problem is that I can't run it. I tried saving it as .html and then running it with mozilla but then I got the whole code written into the mozilla window.
I mean if the code is
var x = 5;
console.log(x);
I get this whole thing written there, not executed.
JavaScript needs to be in a <script> tag for a HTML file:
<script>
var x = 5;
console.log(x);
</script>
You can also define the JavaScript in a .js file and reference it by doing:
<script src="myJSFile.js"></script>
In that case you don't need to place <script> tags inside the .js file.
Maybe you are not showing file extension in windows, and you think that you are saving it in "index.html" but you are saving as "index.html.txt" and mozilla opens it as a text file
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.
I am trying to use an external JavaScript file in my HTML as follows:
<script src='MyScript.js'></script>
The issue is, I am getting the following error in the Web console:
Loading failed for the <script> with source "file:///F:/GIS%20Assessment%202/MyScript.js".
I have realised that MyScript is not saved as having the JavaScript extension, in my documents it says it's a "File" rather than a JavaScript file. I have tried changing the language to JavaScript and saving as a JavaScript file as soon as I save it in my folder it just converts to a generic file!
What do I do? How do I fix this and save my scripts as JavaScript?
This isn't really a programming question, and is likely to be closed, but to force it to have the extension, simply enclose the filename (with the extension) in quotes.
"MyScript.js"
I know this post is over a year old, but I found it worked when I typed the file extension in capitals when saving the file: "myfile.JS" instead of "myfile.js"
I'm writing html code that involves js code. Below a simple example:
<!DOCTYPE html PUBLIC >
<html>
<body>
<h2>Use JavaScript to Change Text</h2>
<p>This example writes "Hello JavaScript!" into an HTML element with id="demo":</p>
<p id="demo"></p>
<script>
var net = require('net');
var sleep = require('sleep');
var element = document.getElementById("demo");
element.innerHTML = "Hello JavaScript!";
sleep(1)
</script>
Unfortunately "Hello JavaScript!" doesn't appear when I browse the above file with my browser. Looking inside the debug console I can see the folowing message:
ReferenceError: require is not defined
So it seems that require is not defined inside the html code but I've written a small test.js file as below:
var net = require('net');
var sleep = require('sleep');
sleep.sleep(1)
and then I run it with
node test.js.
No errors, everything works fine, require is available and sleep and net as well. Why the code inside html file doesn't work?
Javascript is not the same as node.js
require() is not a part of JavaScript standard and is not supported by browsers out of the box, it is the node.js module system.
You might need to directly include the modules; some of the modules might not work in the browser sandbox context.
Also, tools such as http://browserify.org/ might be useful.
The reason you are getting ReferenceError: require is not defined is because nowhere in your html page is Require included. Require does not come with your standard JavaScript library. You must include the file on your page so it can be loaded and used.
This can be done by simply adding <script src="myJS.js"></script> in the <head> or <body> tags. The myJS.js file will, of course, be replaced by the require.js file.
The reason it works when you run with node is because Node already has its own module loader.
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.
So far I have a html link running javascript code, but I am unsure what to do next.
Basically I want to:
Press hyperlink
Run JavaScript Code
Read File
Write File to local directory on the iPad
Here is my code so far:
<a id="mylink"><img src="../assets/images/research/Download.jpg"></a>
<script type="text/javascript">
window.onload = function() {
var a = document.getElementById("mylink");
a.onclick = function() {
console.log("WORKED");
//here is where it will read/write the file
}
}
</script>
I have seen a lot of examples in objective c with webview, but I have tried these and it messes up the layout of the app.
Look in the Quartz 2D Programming Guide in the Apple Docs for Creating a PDF File
The code listing for MyCreatePDFFile provides code for saving out a CGRect to a PDF. I've adapted this into a UIView+PDF category in the past.