This question already has answers here:
Persist variables between page loads
(4 answers)
Closed 4 years ago.
Essentially I have one HTML Script, in the javascript portion of the HTML code, I have a few variables. Essentially how do I get those values to interact with another url location.
To rephrase, Let's say I have this URL, containing the variables in that script: www.example.com/stuff. Now I want to use those variables to interact with www.example.com/stuff2.
How would I do that?
You can use locaStorage or sessionStorage for that:
In one page use:
<script>
let var1 = "some data";
let var2 = "other data";
localStorage.setItem("data", {var1, var2});
</script>
In the other page get the data previously saved using:
<script>
let data = localStorage.getItem("data");
console.log(data.var1);
console.log(data.var2);
</script>
Note: The same syntax works for sessionStorage
If I correctly interpret your question, the solution would be to link the javascript also on the second page with <script> </ script>
You can assign variable globally
file 1
window.var1 ="value"
file 2
window.var2 = window.var1;
Related
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 1 year ago.
I am new in PHP and javascript and need help with the following code
I want to get the value from browser URL that tag with "org" and use it in javascript; as example "http://localhost/nmt/index.php?org=table1" I want to put the "org" value inside the URL/ inside the POST $.post("ch1.php?org=table1", instead of table1 put the GET value
This is the part from code
$(document).ready(function () { showGraph1(); }); function showGraph1() { { $.post("ch1.php?org=table1", function (data) { console.log(data);
That If possible replace table1 with the value from URL
You can inject your variable into the javascript.
<script>
var data = <?php echo json_encode($_GET["org"]); ?>;
</script>
You also can get the url parameter directly from js.
This question already has answers here:
Link to Flask static files with url_for
(2 answers)
Closed 2 years ago.
Please refer to this post where a similar question is already answered: Link to Flask static files with url_for .
I have a simple embed html block that relates to a filepath.
<embed src="/static/filename.txt">
However the filename changes(the path stays the same) as the content inside file changes time to time. Hence, I am currently passing the filename to the html template with the following code(flask)
return render_template('view.html', filename = filename )
Is there way to pass the file name in to embed html tag? The goal result to be something like this?
<embed src="/static/{filename}.txt">
where the {filename} changes based on passed data from flask.
Any help or suggestion of implementation is appreciated!
Yes.
<embed src="/static/{{filename}}.txt">
Did you try using template strings? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
It allows you to insert a variable inside a string.
Your example would look something like this:
<embed src=`/static/${filename}.txt`>
This question already has answers here:
Pass Javascript Value to Java in JSP
(2 answers)
Closed 7 years ago.
New to learning JSP, and trying out passing data between two pages.
I'm wondering if it is possible to pass a javascript variable to session.setAttribute()
At the moment, I can pass a string of text through 2 jsp files like so:
JSP1:
<% String text = "hello";
session.setAttribute("test", text);%>
JSP2:
var someText = "<%=session.getAttribute("test")%>"
which works fine.
However, is it possible to pass through a var into session.setAttribute instead? I store some data in a javascript variable and would like to send it across to the second JSP file.
So for example:
var number = 7;
<%session.setAttribute("test", number);%>
I've tried this out and I get the error "number cannot be resolved to a variable"
Thanks!
You cannot do that since javascript executes on client & JSP executes on server side.
If you want to set javascript variable to JSP session, then you pass this variable through the URL like this
var number = 7;
window.location="http://example.com/index.jsp?param="+number;
Now receive this var in your JSP page like this
String var = request.getParameter("param");
Now set it in session
session.setAttribute("test", var);
EDIT :
var number = 7;
<%session.setAttribute("test", number);%>
In the above code, server will only execute the code inside <% %>. It does not know anything outside of the JSP tags. So, it will also dont know about your javascript variable number.
Server executes the code & the result will be sent to the browser, then your browser will execute that javascript code var number=7;.
Hope, now it is clear for you.
This question already has answers here:
Replace function not replacing [duplicate]
(2 answers)
Closed 8 years ago.
I have written a simple code in a .js file (in Zend framework, but I dont think it matters)
var location = "localhost:8080/mymodule/id/1#";
location.replace(/\#/g, "");
alert(location.valueOf());
return;
but I dont know why I can not see the result I want.
I get my page url and want to omit all number signs appears in it. but the code above does nothing.
please help
location is a bad name to use for a variable since it collides with the window.location variable used for the actual browser page location.
If you change location to loc in your above code, and then also add loc = in front of the loc.replace() call (since replace() doesn't modify the input, but instead returns the new version), your code works.
replace will not change the value of the original string, you need to assign the result to a new variable -
var newString = location.replace(/#/g, "");
alert(newString);
Demo - http://jsfiddle.net/5H5uZ/
It can be done in one line. This is the result you look for?
alert("localhost:8080/mymodule/id/1#".replace(/#/g,''));
//=> alerts 'localhost:8080/mymodule/id/1'
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to rewrite URL without refresh, like GitHub.com
let say I have variable in Javascript
var currentpage = 0;
my address page address is www.xyz.com/page/1
when I click somewhere and js function trigger
currentpage = 1
//Here I want to change/rewrite www.xyz.com/page/2
//Without redirecting to this page
See pushState (browser support currently limited).
You can't. You have to use the '#' or '#!' notation and pass the page number after it, then do trigger some js on load to figure out which page to display.