Why i cant execute this code(js in html)? [duplicate] - javascript

This question already has answers here:
Why can't I call a function named clear from an onclick attribute?
(3 answers)
Closed 9 months ago.
htmlfile:
<!DOCTYPE html>
<head>
<script src="script.js"></script>
</head>
<body>
<div class="q" id="cooki" onclick="cookie()">Accept!</div>
</body>
js:
function cookie() {
alert("hi");
}
and I even tested with console.log, I used script source in body tags , nothing worked

Hi #Z3N1X Welcome to Stackoverflow.
To fix your issue try renaming your function to something else,
since how all of the comments below the answer has said(Thanks for correcting me)
when you use the cookie name in the function or variable the onclick thinks your trying to use the document.cookie function.
that's why this is happening.

Related

Javascript runs before HTML even if at end of body [duplicate]

This question already has answers here:
How to make the HTML renders before the alert is triggered?
(4 answers)
placing <script> tags right before </body> doesn't work as expected
(3 answers)
Closed 1 year ago.
My javascript is running before my HTML loads, even if I put it right before the like this:
<body>
<h1>Title</h1>
<script>
alert("yo");
</script>
</body>
Any ideas why that may be happening?
Any ideas why that may be happening?
alert is blocking, script evaluation comes before DomContentLoaded (DCL) and subsequent first content paint (FCP) which is why it doesn't show Title before alert starts blocking the dom.
You could run it this way:
<body onload="alert('yo')">
<h1>Title</h1>
</body>
Or this way:
<body onload="my_function()">
<h1>Title</h1>
<script>
function my_function() {
alert('yo');
}
</script>
</body>

How to call javascript and write it on html? [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 3 years ago.
so I'm using this Javascript (API) that has a bunch of function. How do I go about writing it to an HTML file?
so one of the function is "api.ping()" which works fine on powershell, but I cant get it to print that in an HTML file.
So in the script I wrote
document.getElementById("demo").innerHTML = api.ping();
and the HTML is
<!DOCTYPE html>
<html>
<body>
<script type="index.js"></script>
<p id="demo"></p>
</body>
</html>
I'm trying to put the value returned from the call onto the HTML file.
I think your index.js file is probably not being included. You'll want to change the tag line to read:
<script type="text/javascript" src="index.js"></script>
Assuming that index.js is in the same directory as this HTML file.

document.getElementById() does not work inside function closure [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 3 years ago.
I'm trying to use function closure in my javascript as follows:
In my html file:
<head>
<script src="myscript.js"></script>
</head>
<body>
<section id="mysectionId"></section>
</body>
In myscript.js:
(function() {
var id = document.getElementById('mysectionId');
console.log(id);
}());
However, id seems to equal null. I'm not sure what I've done wrong - does function closure scope exclude globals like 'document'? If so, how come I can still use 'console.log()' inside the function closure?
You're javascript is running before your html loads. Put your script tag after your html content at the very bottom of the body
<head>
// put css here
</head>
<body>
<section id="mysection"></section>
<script src="myscript.js"></script>
</body>

Basic code not working with jQuery [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 6 years ago.
I am very new to jQuery, literally just trying to get it to work for the first time. Only the alert box works, but none of the other very simple methods I am trying out. Here are the files:
HTML:
<!DOCTYPE HTML>
<html>
<head>
<title>jquery</title>
<script type="text/javascript" src="jquery-3.1.0.min.js"></script>
<script type="text/javascript" src="myJavaScript.js"></script>
</head>
<body>
<h1 id="title">This is a title</h1>
<p> This is some sample text.</p>
</body>
</html>
JavaScript file:
$(document).ready(function (){alert("this works!")});
$("#title").text("but this is not not working");
As you can see the example can't be simpler. I have off course downloaded the jquery-3.1.0.min.js file and put it in the same folder as the html. If I comment out the link, the alert stops working, so I now the file is being referenced OK. Why is the second line of jQuery not working? Many thanks, P
Because the DOM(the entire structure of your application) isn't loaded yet. You need to use a $(document).ready(makes sure the DOM is loaded before running jQuery code) or an event handler to make the change visible.

Java script code works only when I place it in the body section [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 6 years ago.
I don't understand why this simple script works only when I place it in the body?
when i place the script within the head or in external JS file it doesn't work.
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My First Paragraph</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>
</body>
</html>
Because the element you reference is defined only later (at the time the <head> is processed the browser hasn't processed the markup underneath it yet).

Categories