I am working with the html5 history api. When I click a link to a new page, and then click the back button. I am seeing variables who are retaining their state between page loads.
Example:
(function(namespace){
var singlePageSessionId = 0;
singlePageSessionId = new Date().getTime();
namespace.SinglePageSessionId = function(){
return singlePageSessionId;
}
}(window.namespace = window.namespace || {}))
That singlePageSessionId variable should be set once per actual page load. However when I click back, and then check the value of namespace.SinglePageSessionId it retains the timestamp of when I first navigated to the site. Since I am re-executing the javascript I expect the variable to be reset.
I either have a fundamental mis-understanding of how the javascript engine works between pages or there is some weird behavior here.
Is there a way to make sure the variables get reset ?
How hard I tried I could not reproduce the issue you are talking about.
Locally I had no problem at all going back and receive new value.
This is what I did http://jsfiddle.net/itaymer/bqg3azx8/
(function(namespace) {
var singlePageSessionId = new Date().getTime();
namespace.SinglePageSessionId = function(){
return singlePageSessionId;
}
}(window.namespace = window.namespace || {}));
document.querySelector(".pageSessionId").innerHTML = window.namespace.SinglePageSessionId();
Maybe you failed to supply the whole situation information?
Related
EDIT: the issue has been resolved. Apparently, the page didn't load fast enough for the id referenced to to be in place. Adding a event listener as follows solved the issue:
window.addEventListener('load', function() {stuff that's supposed to happen})
on my clueless journey to creating a little game I'm currently battling saving to and loading from the user's local storage. While saving appears to be working, loading doesn't, unless I manually put the respective commands into the console.
Here's the code snippet that's acting up:
//definition of variables
var dayLength = 10;
//if function to check if there's stored data, loading it and changing the document accordingly
if ( localStorage.saveExists === "true" ) {
dayLength = Number(localStorage.dayLength);
document.getElementById("displayDayLength").innerHTML = dayLength.toFixed(3);
}
var loadingAttempted = "true";
//function to save which occurs every 15 seconds (or through a button click)
function saveAuto() {
if ( loadingAttempted === "true" ) {
var saveExists = "false";
localStorage.saveExists = "true";
localStorage.dayLength = dayLength;
}
When no data are in the local storage (in other words, if saveExists = "false") the site won't attempt to load something and works as intended. The moment there is data in the local storage, the file attempts to load it (so thus far it works).
However, I'll get an error in the line supposed to change the document according to the loaded data, claiming that dayLength is "null".
If I then manually input localStorage.dayLength into the console, it returns "10", as it should. I know that storing the data into the local storage works, because otherwise the if function for loading wouldn't know (saveExists === "true") and thus not do anything. For whatever reason though, it fails to load any other data correctly and returns "null", unless I manually put the exact same code into the console.
Best part is, that I got the whole idea from another little game that does the exact same thing and there it works flawlessly.
I'm at a total loss as to why it works at the other project, but not with mine.
Educate me, if you will. And thank you in advance.
PS: I tried to use localStorage.setItem and localStorage.getItem also, but the issue remained.
This is the first time I get my hands on with automation instruments in xcode The script works well for all button taps but the one making server connection. I don't know the reason
Here is the script I tried so far
var target = UIATarget.localTarget();
target.pushTimeout(4);
target.popTimeout();
var window=target.frontMostApp().mainWindow()
var appScroll=window.scrollViews()[0];
appScroll.logElementTree();
UIATarget.localTarget().delay(2);
appScroll.buttons()[1].tap();
The above script works up to showing the UIActivityIndicator instead of moving to next controller after success
I know There must be a very simple point I am missing. So help me out
UIAutomation attempts to make things "easy" for the developer, but in doing so it can make things very confusing. It sounds like you're getting a reference to window, waiting for a button to appear, then executing .tap() on that button.
I see that you've already considered messing with target.pushTimeout(), which is related to your issue. The timeout system lets you do something that would be impossible in any sane system: get a reference to an element before it exists. I suspect that behind-the-scenes, UIAutomation repeatedly attempts to get the reference you want -- as long as the timeout will allow.
So, in the example you've posted, it's possible for this "feature" to actually hurt you.
var window=target.frontMostApp().mainWindow()
var appScroll=window.scrollViews()[0];
UIATarget.localTarget().delay(2);
appScroll.buttons()[1].tap();
What if the view changes during the 2-second delay? Your reference to target.frontMostApp().mainWindow.scrollViews()[0] may be invalid, or it may not point to the object you think you're pointing at.
We got around this in our Illuminator framework by forgetting about the timeout system altogether, and just manually re-evaluating a given reference until it actually returns something. We called it waitForChildExistence, but the functionality is basically as follows:
var myTimeout = 3; // how long we want to wait
// this function selects an element
// relative to a parent element (target) that we will pass in
var selectorFn = function (myTarget) {
var ret = myTarget.frontMostApp().mainWindow.scrollViews()[0];
// assert that ret exists, is visible, etc
return ret;
}
// re-evaluate our selector until we get something
var element = null;
var later = get_current_time() + myTimeout;
while (element === null && get_current_time() < later) {
try {
element = selectorFn(target);
} catch (e) {
// must not have worked
}
}
// check whether element is still null
// do something with element
For cases where there is a temporary progress dialog, this code will simply wait for it to disappear before successfully returning the element you want.
I'm using the Drupal module "Autologout." https://drupal.org/project/autologout
This module has a timer. When there is no activity on the page for a prescribed amount of time, it kills your session.
I would like to be able to manipulate the timer value. I looked throught the Drupal object in javascript but I can't seem to find where the value is stored. I would like to be able to set activityResetTimer to 0, for example. It seems to be declared here:
autologout.js
(function ($) {
Drupal.behaviors.autologout = {
attach: function(context, settings) {
console.log("This is happening");
if (context != document) {
console.log("CONTEXT IS NOT DOCUMENTS");
return;
}
var paddingTimer;
var t;
var theDialog;
var localSettings;
// Activity is a boolean used to detect a user has
// interacted with the page.
var activity;
// Timer to keep track of activity resets.
var activityResetTimer;
I have looked everywhere (seemingly) in the Drupal js object, but nowhere do I see the activityResetTimer.; http://pastebin.com/PYD2bfcP If you need me to share this in some other way, let me know, I can edit. Also let me know if you need more information or details.
Much appreciated.
Due the nature of this module, it doesn't just store a local variable with a countdown. It uses some Ajax request, form time to time, to check if it should keep the user logged.
If you would like to rewrite the behavior of this counter or interfere on how it works, you should take a look at the module's API. Take a look at autologout.api.php file to see if it has the methods you can use.
I am trying to use $.post to send form data to a server side script to be saved if the user tries to leave the page without submitting the form. I am using the same function attached to a save button and on setInterval set to every 2 minutes, and it works fine. But when I attach the function to document.onbeforeunload it does not work. In firebug, I see the request is being sent, but it looks like it is being stopped before a status code is returned and the page continues to unload. I am still pretty new to Javascript and Jquery and I am not sure if maybe $.post is one of those functions that might not work on the onbeforeunload event. If that is true, is there another way I can send the data if the user tries to leave the page without saving?
This is the function I am calling from the onbeforeunload event:
function ajaxSubmit(){
var blogtitle = $("#title").val();
var publishedstate = 0;
var blogid = $("#blogID").val();
var blogbody = CKEDITOR.instances['body'].getData();
var postdata = {ajaxSubmit:true,title:blogtitle,body:blogbody,published:publishedstate,blog_id:blogid};
$.post('ajaxblog.php',postdata,function(data){
$("#autosaveMessage").html(data);
$("#autosaveMessage").show();
setTimeout(function(){$("#autosaveMessage").hide();},5000);
});
}
and this is how I am calling the function:
var post_clicked = false;
$("#postButton").click(function(){
post_clicked = true;
});
function leaveEditor(){
if(post_clicked==false){
ajaxSubmit();
}
else{
//Do Nothing
}
}
window.onbeforeunload = leaveEditor;
No, and this is by design. It would be remarkably troublesome if a page could use onbeforeunload to indefinitely delay browsing away, persist its presence somehow, etc. One of the most important abilities for a user of a web browser to have is the ability to leave.
Just use the stringy return value—the whole point of it is to remind the user that s/he made changes that will be lost. Like on SO :)
Can we use a Global variable that persists over multiple instances of the browser (FF)?
I am building a ff extension which has to take the host& port name from the user once and then execute the menu options accordingly using that very host and port. This Host and port must remain same untill the user resets it (for which an option will be given)
On declaring the variable as global in the JS file, it would become null everytime the browser is restarted. Can anyone help me out with how and where to save this variable to get the desired functionality. Heres the code to set the preferences. but doesnt work for me
function setInstance() {
if (pref_manager.prefHasUserValue("myvar")) {
getString = pref_manager.getString("myvar");
instance = getString;
}
if (instance == null) {
instance = prompt("Please enter webcenter host and port");
// Setting the values
pref_manager.setString("myvar", instance);
pref_manager.setIntPref("myintvar", 1);
}
}
This function is called as soon as the extension menu option is opened. instance is a global variable which i need to be inputed by the user only once till reset
This is a very old post but still. There are two bits in the given code that don't seem to work.
This code snippet won't work for me:
getString = pref_manager.getString("myvar");
Instead I used the one below and that works for me:
getString = pref_manager.getCharPref("myvar");
Same applies to setter. This won't work for me:
pref_manager.setString("myvar", "mystring");
But this does:
pref_manager.setCharPref("myvar", "mystring");
See: http://www.rietta.com/firefox/Tutorial/prefs.html
You can store it in Firefox's preferences (so it gets stored in about: config and will be available every time Firefox is loaded).
var pref_manager =
Components.classes["#mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
// Setting the values
pref_manager.setCharPref("myvar", "mystring");
pref_manager.setIntPref("myintvar", 1);
// Getting the values
var getString = ""; // Default
if (pref_manager.prefHasUserValue("myvar"))
{
getString = pref_manager.getCharPref("myvar");
}
var getInt = 0; // Default
if (pref_manager.prefHasUserValue("myintvar"))
{
getInt = pref_manager.getIntPref("myintvar");
}
You can find more information on the Mozilla Developer Center page for Preferences, and Adding Preferences To An Extension Page.