Send data to another page - html [duplicate] - javascript

This question already has answers here:
How to pass variable value between different html pages in javascript
(5 answers)
Closed 3 years ago.
Hello I wanted to send data from one html page to another
For eg this is my index.html page
<html>
<body>
<script>
var uname = "karan"; // I want to send this
</script>
</body>
</html>
And this is newPage.html:
<html>
<body>
<script>
console.log(uname); // I want the uname form index.html
</script>
</body>
</html>
I have already tried declaring a new class in another javascript file but it gives undefined
Is there any way I can do this? Thank you

use session storage like this:
sessionStorage.setItem("uname", "karan");
and retrieve it like this on another page:
console.log(sessionStorage.getItem("uname"));

Related

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

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.

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>

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).

Call a Javascript function on load of JSP

Can i call a javascript function on load of a JSP file according to a request parameter value?
I want to send a request from my Servelt to the JSP and if the value of a parameter="something" i want to call a specific JS function.
Is that possible? Please send me any relevant example if it is available.
thnks in advance
According to your question i assume this is what you want try it
<%
String text = request.getParameter("parameter");
%>
<html>
<head>
<script>
var text1="<%=text%>";
if(text1.length>0)
{
//do what you want call function or something
}
</script>
</head>
<body>
</body>
<html>

Categories