Javascript and session variables - javascript

I have a database that stores events in it and a page with a calendar object on it. When rendering the days it looks through the months events and if any match the current day being rendered it creates a linkbutton to represent the event in the day on the calendar and adds it to that cell. I add some javascript to the linkbutton to change the window.location to a page to view event details passing EventID in the querystring ( I tried setting the postbackurl of the newly created linkbutton but it wasnt causing a postback... no luck). I need to set a Session variable ie. Session("EditMode") = "Edit" So the new page will know it is to get an existing event info rather than prepare to create a new event? Any SUGGESTIONS?

Your session vars are controlled by the server,
JS runs client side, and as such cannot modify the vars directly.
You need to make server requests using POST or GET and hidden
iframes, or XMLHTTPRequest() calls to send data from the JS to
the server, and then have your server side code handle the vars.
Add another query string variable that the page can use to trigger existing vs new.

Add another query string variable that the page can use to trigger existing vs new.

If you are using something like Struts2, you can have a hidden variable in your jsp
<s:hidden id="EditModeId" value="%{#session.EditMode}"/>
And within javascript simply access this variable
alert(document.getElementById('EditModeId').value);

You definitely need to add a variable to the target page. But I take it that you are doing a popup scenario, so you should be able to create a javascript function OpenWindow() and fire it off when the user clicks the link.
<script>
function OpenWindow(eventId, editMode)
{
var window = window.open("popup.aspx?eventId=" + eventId + "&editMode=" + editMode);
}
</script>
On the server side you need to build the call to the OpenWindow function. For example:
onclick="OpenWindow(eventId=" + row["eventId"].ToString() + "&editMode=" + editMode.ToString() + ");"
So in other words, prep everything on the serverside to set your javascript to post all variables to the new page. Hope this helps.

var page1 = document.getElementById("textbox").value;
sessionStorage.setItem("page1content", page1);
in other page use this value as like session variable
document.getElementById("textbox2").value=sessionStorage.getItem("page1content");

Related

Call function before the href Vuejs

I'm trying to call a function on a click event before the href is invoked. I want to do this so that I can set a value to a given variable and then when I redirect to the new page I can access that variable via this.$parent.varName. This is what I'm trying to do but it won't call the function:
<a #click="openWorkflow(rec.id)" href="#/workflows">Edit</a>
Not sure what I'm doing wrong here and/or if this is the right way to achieve what I want
I'd take care of redirecting to the next page inside the openWorkflow function, then you can place it directly wherever you need it to be in the logic. If you're using vue-router, you can do this by just running router.push("#/workflows"). If you're not, you can still do the standard window.location.href = 'baseurl.com/#workflows'.

Unable to figure out why input parameter is becoming null

This is the script I have in my gsp page:
<script>
function getItemsLength(){
var id = document.getElementsByName("franchiseID")[0].value();
alert(id); //This displays the intended id
var itemLength = ${storeCommand.numOfBranches(id)}; //The id becomes null when sent here
}
</script>
This function is called onclick for a button, I cannot access the id through the store command itself, since the page hasn't been saved yet - it appears as null. Essentially I take in an ID the user gives me for the franchise, and I query to see how many stores have that franchiseID to display it on the screen.
I am new to grails and web development, so let me know if there is anything I am doing wrong!
The fundamental issue you have here is you are trying to mix client-side programming and server-side programming. ${storeCommand.numBranches(id)} is processed when the HTML is rendered and in that case id is not in scope.

Function uses an non-updated version of variable

I have a web-app (GAS) where once the user has logged in (through Firebase/Google Auth), i am able catch his uID. Since i need this uID on server-sided functions later, once i catch it i run a function via google.script.run that will update an already declared global variable so that to store it on the other side. This variable is declared outside any function as empty like this var fbuserIDkey = ""; when the app is loaded . So this variable, once the user has logged in, should have been updated with the uID.
My problem is that later i need this variable in a function triggered by a click on a button, but the function server side seems to use an old version of the variable if that is possible, like it has not been updated.
Here are the bits of code involved:
declaring variable:
var fbuserIDkey = "";
passing the value when user is logged and updating server-side variable :
onAuthStateChanged{
...
const userIDkey = user.uid;
...
google.script.run.logUser(userIDkey);
} //console.log(userIDkey); = correct value
the function that updates global variable server-side:
function logUser(userIDkey){
fbuserIDkey = userIDkey;
Logger.log("ID is " + fbuserIDkey);//returns correct value on server-side too
}
So the variable has been updated.
But later, when the button is clicked on client-side and it triggers the function that should use the variable on server-side, it is still empty. Using Logger.log(fbuserIDkey) returns ""
It looks like the variable has been updated but the function that actually needs it has the old version of it (""). How is that possible? I precise that the click on the button always happens far after everything is loaded. I guess there might be some trick due to server/client communication but i don't understand where exactly and how to correct it.
EDIT
So as suggested i add more context, trying to paste as much code as possible.
fbuserIDkey is declared on the file (.gs) that contains the doGet() function if it adds any relevant detail.
var fbuserIDkey = "";
function doGet(e) {
...}
below is the function triggered by button that uses the variable:
function TransferPicks(picks){
Logger.log("ID is" + fbuserIDkey);//returns ""
...}
the eventlister is:
document.getElementById("savepicks").addEventListener("click", StartSaving);
StartSaving is a function that does several stuff (not related) and in the end triggers TransferPicks
Just answering the question since it is now solved if that can help anybody.
The trick as advised by the comments was indeed to get (again) the user.uID right at the moment I (re)need it, instead of trying to store it once and use it later. So, when document.getElementById("savepicks").addEventListener("click", StartSaving); happens, i get user info with onAuthStateChanged((user) => {if (user) {...}} and create one object with all the data I need from the client side. Then I pass it through google.script.run to the server side function and it works just fine!
Thanks to all who commented, I was just forcing it the wrong way.

Create Multiple Instances Of Same Script

I am currently building a website that uses windows to load in new content via ajax. These windows are allowed to contain the same page as in another window using the same javascript. Currently I assign a unique id to the new window which it then stores for later use.
Once the code is loaded in, all the ids in that window are converted by adding on to them a unique_id. ie "box" becomes "box_win1". I then send this id to the javascript by assigning it to a variable so it can be used in document.ready function.
The pseudo code for the window is like the following:
document.ready{
var temp_id=id+1;
$("#mybox" + temp_id).val("abc")
//run some startup stuff
}
I am just wondering is there a better way to do this. As I find if I open to many new windows all at once the temp_id conflicts and goes to the wrong window.
I would like to some how create an instance of the code but I am not sure how. I cannot use global functions however as that may cause naming conflicts.
put this into a function
function callMe (){
var temp_id=id+1;
$("#mybox" + temp_id).val("abc")
//run some startup stuff
}
you can use callMe() anywhere then

PrimeFaces get the current page number on page change event

I have a primefaces datatable.
I need to know the current page number on page change event using client side API.
I am using,
<p:ajax event="page" oncomplete="myFunction(usersWidget);"/>
Inside myFunction() i have
debugger;
console.log(usersWidget.paginator.getCurrentPage());
The problem is, callbacks inside oncomplete of page event are called before the new page is set (PrimeFaces.widget.Paginator.setPage) on the paginator. I traced this with the debugger.
That being said, i would get the previous page number i was on and not the current page number, inside oncomplete callback.
If i could get a callback after the page is set after i click the page link, i would know the current page.
EDIT: Docs say that its a callback called after ajax completion and after DOM is UPDATED.
then what could be potentially wrong?
Pointers please?
Its strange , cause oncomplete being called after the page was updated...
How about calling oncomplete="myFunction();" without passing the parameter ?
You might be passing the old object (with outdated information) that way... the usersWidget is global variable anyway... ans should be present in your js file
Since it seems that client side api is not synced with the right values
As a workaround you could try to pass the page value from server like this
DataTable dataTable = (DataTable)
FacesContext
.getCurrentInstance()
.getViewRoot()
.findComponent("someOformID:someTableId");
or try, instead of the line above,
PageEvent.getPage(); //PageEvent is an argument to you listener
myPageHolder = dataTable.getPage();
Than place this value to bean property and put it inside some hidden value in xhtml , than update that hidden value with p:ajax update and access it from within js

Categories