Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
document.write("Oops! The document disappeared!");
}
</script>
</body>
</html>
The above code is showing output like below
My First Web Page
Try it
After clicking Try it button I got output like below
Oops! The document disappeared!
In internet i found the reason for disappear is If I execute document.write after the document has finished loading, the entire HTML page will be overwritten.
but my doubt is when document has finished loadind...........i am not gettimg it.
Then I modified the above code like below
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<button onclick="myFunction()">Try it</button>
<script>
function myfunction()
{
document.write("My First JavaScript");
}
</script>
</body>
</html>
but the above code is showing output like below
My First Web Page
Try it
After pressing Try it button it is not giving any output.I don't know why these 2 program are working like this.help me..................
Second one is failing because you have a small f in myfunction and call it with a big F
In Chrome, if you open Developer tools ( Cntrl-Shift-J ) and go to the console it will show you the javascript error.
The problem in the second page is that you are calling a function called myFunction.
But the function that you have is called myfunction, the F is in Uppercase.
so the javascript error is:
Uncaught ReferenceError: myFunction is not defined
JavaScript is case sensitive. You define
myfunction
But call
myFunction
Note the F.
In your second function, myfunction, F should be CAPS or at onclick=myFunction() F should be small. Check your console window. No output is because of javascript errors
Related
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>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm new to js and I was working on something where this thing makes a bunch of new tabs, but when the code is run, the alert pops up when each tab is opened and on each individual tab.
here's the code
<script>
alert('something')
setInterval(function() {
var w = window.open();
w.document.write(document.documentElement.outerHTML||document.document
Element.innerHTML);
}, );
</script>
pls be nice I'm new
What about this one
<!DOCTYPE html>
<html>
<body>
<p>Click the button to open a new browser window.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
alert("Hi there");
window.open("https://www.w3schools.com");
}
</script>
</body>
</html>
Sincere apologies if this has already been covered. I've been trawling StackOverflow for a while now and I cannot find anything that will fix my issue. Many people have this error, but theirs seem to be derived from syntax errors and the suchlike. I (nor JSLint) can't find any such errors, so I'm rather stumped.
The following code (from the W3Schools page) does work:
<p>Click the button to trigger a function that will output "Hello World" in a p element with id="demo".</p>
<button onclick="myFunction()">Click me</button>
<p id="demo"></p>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World";
}
</script>
However, my code does not work:
<head>
<!--SNIP-->
<script language="text/javascript">
function iconclicked() {
alert("Yay!");
}
</script>
</head>
<body>
<div class="icon" id="misc" onclick="iconclicked()"></div>
</body>
This produces the "Uncaught ReferenceError: iconclicked is not defined" error.
But, and correct me if I'm wrong, function iconclicked() {...} is what defines the function and it is clearly present. Additionally, moving the JScript to be within the <body>tag does not fix the problem, regardless of whether to script is before or after the div. If you were to substitute the div for another element (a button, image, text, etc) or to call the function a different way (e.g. via onload) the code does not work.
I thought that perhaps it was the rest of the code on my page causing something funny to happen, so I headed over to JSFIDDLE and popped the old W3Schools code in there. Lo and behold, "Uncaught ReferenceError: myFunction is not defined". JSFDDLE link
Edit: turns out this is just due to the fact I didn't choose 'no wrap' in the JSFIDDLE options - thanks Peter Healy :)
But if you go ahead put the JS inside <script> </script> tags in the HTML section of JSFIDDLE, there is no such problem and the code works as intended. JSFIDDLE link
I figure maybe this is a scope problem, but I can't for the life of me work out what it is specifically. Hopefully someone out there with more experience has the answer!
There is no language of "text/javascript", that is the type.
Change
<script language="text/javascript">
to just
<script>
Do not worry about the language or type attributes. Language has been deprecated.
It's actually <script type="text/javascript">, with a type attribute, not language:
<head>
<!--SNIP-->
<script type="text/javascript">
function iconclicked() {
alert("Yay!");
}
</script>
</head>
<body>
<div class="icon" id="misc" onclick="iconclicked()"></div>
</body>
The language attribute went out of style a while ago.
It's how JSFiddle wraps up your JS. Choosing No Wrap option on left hand side will solve it for you.
Read here: no wrap
Edit: remove text/javascript
This question already has answers here:
when and where to put javascript in html
(7 answers)
Closed 9 years ago.
the thing is i'm unable to figure out where to embed javascript in html page whether in head section or body section.
example 1:
<html>
<head>
<title>events</title>
<script>
document.getElementById("b").onclick=function(){displayDate()};
function displayDate()
{
document.getElementById("demo").innerHTML=Date();
}
</script>
</head>
<body>
<p id="demo"></p>
<button id="b">new</button>
</body>
</html>
in the above example I placed script tags in head section but it is not working.
example: 2
<html>
<head>
<title>events</title>
<script>
function upper()
{
var x=document.getElementById("t");
x.value=x.value.toUpperCase();
}
</script>
</head>
<body >
enter some text:<input type="text" id="t" onChange="upper()"/>
</body>
</html>
in the second example I placed the javascript in head section it is working properly.first example demonstrates that on clicking a button date will be displayed in the second example in a text box when data is entered and if we come out of the box the letters in the box will we converted to uppercase.
To have it more readable I prefer to always place JavaScript in the head section. If you need to access elements from there, use the window.onload event:
<head>
<title>events</title>
<script type="text/javascript">
window.onload = function() {
document.getElementById("b").onclick = function() {
displayDate();
};
};
function displayDate()
{
document.getElementById("demo").innerHTML=Date();
}
</script>
</head>
This would work just fine.
Your second example worked because you just defined a function, you didn't try to access any element.
You can put it in the head. The problem is that your examples are not the same. The first one doesn't work because the current date is retrieved by calling Date(), when it should be new Data().getDate(). The second example works because the code is valid.
The problem you're running into is that you're trying to reference an element before it is loaded into the DOM.
When you're putting the script in the HEAD tag, the dom hasn't been loaded yet and the document.getElementById won't find what you're looking for.
You have a few different options to deal with this. You can put the script at the end of the page, which will work for your small example here.
Probably a better option is to take a look at learning/using jquery or another js utility. Jquery makes it easy to solve this issue by giving you a "ready" event. This ready event will be triggered when the DOM is fully loaded. So:
$(document).ready(
function()
{
$("#demo").html((new Date()).toString());
});
Is all you really need. With this approach, it doesn't matter where the script it on the page.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is It Possible to Sandbox JavaScript Running In the Browser?
I would like to give the user the ability to input JavaScript in a textarea like this:
<!DOCTYPE html>
<html>
<head>
<script>
function lapzwans()
{
var d = document.getElementById("area").value;
document.getElementById("blahblah").innerHTML = d;
}
</script>
</head>
<body>
<h1>Javascript in Javascript</h1>
<p>This experiment attempts to launch a script from another script.</p>
<p>Enter your script in the text area</p>
<textarea type="text" id="area"></textarea>
<button onclick="lapzwans()">Click here</button>
<p id="blahblah"></p>
<p id="deegroller">Make this text green</p>
</body>
</html>
I have tried this, but I get no result.
Is it impossible to do this, or am I doing things wrong?
Thanks in advance.
One possible answer:
eval(/* some JS here */);
Note though that what you are trying to do can be dangerous (in some cases) as you are allowing user-written arbitrary JS to be executed.
Related reading:
How evil is eval?
When is JavaScript's eval() not evil?
the eval method takes whatever string is passed to it and tries to run it.
eval("alert('foo')");
will alert "foo";
You don't need to use the evil eval to do this.
function lapzwans()
{
var d=document.getElementById("area").value;
document.getElementById("blahblah").innerHTML=d;
var userScript = new Function(d);
userScript();
}