Passing values from one page to another in JavaScript [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
In below code, the value is not passed from one page to another.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<script type="text/javascript">
function generateRow() {
var myvar = "testuser";
'<%Session["temp"] = "' + myvar +'"; %>';
window.location = "WebForm2.aspx";
}
</script>
<head>
<title></title>
</head>
<body>
<div runat="server">
<input id="Button1" type="button" onclick="generateRow()" value="button" />
</div>
</body>
</html>
What is the problem in above code?

You cannot set a session with Javascript. Your output JS file will be exactly what you wrote down.
You can do it like this:
window.location = "WebForm2.aspx?temp=" + myvar;
And in Webform2 you can do:
Request.querystring["temp"];

Global variables in one page are not kept by the next.
If you want this client-side, you have three options (at least):
Pass the variable to the next page on the query string, e.g.:
window.location = "WebForm2.aspx?myvar=" + encodeURIComponent(myvar);
Use a cookie (millions of examples online, I won't repeat them, bit of a pain to read the cookie).
Use client-side session storage or local storage, both of which are covered here. Here's the client-side session storage example:
// Setting
sessionStorage.myvar = myvar;
// Getting (on the next page)
var myvar = sessionStorage.myvar;
(sessionStorage is a global variable provided by the browser.)
This works on just about any browser you need to care about (IE8+, and modern versions of basically everything else).
Server-side, both option 1 and 2 would work, but 1 would make the most sense.

Related

How to escape double(single) quotes inside another double(single) quotes with JavaScript [closed]

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 6 years ago.
Improve this question
I have an input field where the user can enter everything ("", ''). Is there any way I can escape the quotes without previously knowing their type? I mean if the user enter ("test"test'test") how can i store it in js variable ? Any suggestion can help me.
For example, I want this to work properly:
<!DOCTYPE HTML>
<html>
<body>
<script>
var myVar = "test"test'test";
console.log(myVar.replace('\"', '\\"'));
</script>
</body>
</html>
INCORRECT (with comments explaining why):
You cannot do a replace on an already syntactically broken variable declaration. You need to fix the broken syntax.
<!DOCTYPE HTML>
<html>
<body>
<script>
// <-- You CANNOT do this. It's incorrect syntax. You need to escape it appropriately using backslashes.
var myVar = "test"test'test";
// This line is not required. You need to correct the syntax error above instead.
console.log(myVar.replace('\"', '\\"'));
</script>
</body>
</html>
CORRECTED:
Here you can see the correctly escaped values inside the string literal.
<!DOCTYPE HTML>
<html>
<body>
<script>
var myVar = "test\"test\'test";
console.log(myVar);
</script>
</body>
</html>

Unable to access html elements by tag using Java script [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
My html is like this:
<!DOCTYPE html>
<html>
<head>
<title>
Event Handling
</title>
</head>
<body>
<h1> This is the chapter in which I am handling Events.The site you should visit is: </h1>
<b>here<b>
<script src="events.js"></src>
</body>
</html>
The javascript is like this:
var link = document.getElementsByTagName("a")[0];
link.onclick = MyEventHandler;
function MyEventHandler()
{
alert("ouch!");
return false;
}
It is not working, but using inling event handler I am doing it successfully. Please helop me.
You need to assign the event handler function, not the return value of calling that function.
link.onclick = MyEventHandler;
(Also note that you need to use the end tag </script> not </src>.)

calling javascript function from body of html [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a javascrip[t function that is saved in a file (test.js). I want to call the function from within the body of the html file
something like
<script>
load drawLogo("text" val1, val2, val3);
</script>
(since this function will be called on different pages with different values)
Right now I am having to put that call at the bottom of the main function drawLogo() which means I can't customize it.
<script src="text.js" type="text/javascript"></script>
if it is in same function then make it like:-
onclick="func()" or onmmousehover="func()"
define function func() in the same file within script tag
Just put the function name and the parameters that receive this, dont forget first call the js file:
test.js:
function drawLogo(paramText,value1,value2,value3){
//Do something
}
html:
<head>
<script src="test.js" type="text/javascript"></script>
<!--you can put this in the head or in the body-->
<script>
drawLogo("some","value","for","example");
</script>
</head>
<body>
<!--you can put this in the head or in the body-->
<script>
drawLogo("some","value","for","example");
</script>
</body>

Why is my button not doing anything? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am trying to use HTML to call a function in an external JavaScript file. The JavaScript file is called "javascript.js".
Here is my HTML code:
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="javascript.js">
</script>
<script
// Calling the Google Maps API
</script>
<script>
<!-- JavaScript to load Google Maps -->
</script>
</head>
<body>
<div class="content">
<div id="googleMap"></div> <!--don't have to worry about this -->
<div id="right_pane_results">hi</div> <!--don't have to worry about this -->
<div id="bottom_pane_options">
<button onclick="todaydate()">Try It</button>
</div>
</div>
</body>
...and my JavaScript code (something I got from the internet just to test):
function todaydate() {
var today_date=new Date()
var myyear=today_date.getYear()
var mymonth=today_date.getMonth() + 1
var mytoday=today_date.getDate()
document.write("<h1>"+myyear+"/"+mymonth+"/"+mytoday+"/h1">)
}
On my webpage that I'm running locally, the button is showing, but nothing happens when I click on it. Is it something to do with my code?
Thanks in advance,
Josh
There are 2 mistakes inside the function. The > needs to be inside the quotes and there needs to be a < before the /h1.
Replace this line:
document.write("<h1>"+myyear+"/"+mymonth+"/"+mytoday+"/h1">)
with this
document.write("<h1>"+myyear+"/"+mymonth+"/"+mytoday+"</h1>")
and it should work.
I guess this has to do with the syntax error you have:
document.write("<h1>"+myyear+"/"+mymonth+"/"+mytoday+"</h1>")
note how the last quote is after the > (and you also forgot the < for the closing h1)
document.write("<h1>"+myyear+"/"+mymonth+"/"+mytoday+"</h1>")
This works!
As other answer mention syntax issue is in your document.write statement.
Beside this use getFullYear() to get year as getYear() is Deprecated.
some other practices to make your code cleaner:
Use semi-colan at end of statement
Use a good naming convention
Here is Demo
function getTodayDate() {
var todayDate=new Date();
var myYear=todayDate.getFullYear();
var myMonth=todayDate.getMonth() + 1;
var myDay=todayDate.getDate();
document.write("<h1>"+myYear+"/"+myMonth+"/"+myDay+"</h1>");
}

Error about dynamic countdown redirect [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I have an javascript to redirect to another page after countdown 10 seconds, but it doesn't work. Can anyone help to fix the problem?
<body>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type ="text/javascript" src ="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var number = 10;
var url = 'http://localhost/rms/index.php/';
function countdown() {
setTimeout(countdown,1000);
$('#title1').html("Redirecting in " + number + " seconds");
number --;
if(number<0) {
window.location = url;
number = 0;
}
}
countdown();
});
</script>
</head>
<body>
<div id="title1"></div>
</body>
As everybody wrote in the comments, it should work. Try to replace
<script type ="text/javascript" src ="jquery.js"></script>
with this:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
In this case you don't have to worry about your jquery.js file, which could be missing or corrupted.
I have one note to your code. Are you aware of that you call window.location = url; two times?
If its just a regular countdown with redirect I will put the setTimeout call in if statement which will prevent calling it twice.

Categories