I want to store a specific value for each browser window.
This value should stay the same after a reload.
An example:
I want to open 2 tabs of my page, both of them should be able to show the data of two different accounts. E.g. gmail has the ability to swap between 2 logged in pages. And even if you refresh the tab, the visible account stays the same.
At the moment I have only one possible solution in mind:
Store the value in the url
Make sure that every link on this page pass this value to the next page
But this way seems to be a little bit "dirty". It gets broken if I miss a single link.
Is there a way to use a hidden input instead, or some other js features?
Btw: I'm using aspx and IIS as web server.
I think you should use cookie, they stored in browser and stay after reload.
Set cookie and get cookie with JavaScript
Related
the requeirment is that I want to avoid the specific web page to save to bookmark,
and is there someway to acheive this funcion just use some code, maybe add or js code . thanks
The answer is no, the user can always bookmark a page as this is browser function, but you can use sessions. Then make sure that any request for a page
must have an active session id or it returns an error or redirects to the home page. The user can bookmark the page but the bookmarks will then only work for a short time (until the session expires). This also has the added benefit of
making the site impossible to index by search engines.
The closest you're going to get is if you open another window using JavaScript as you can control whether the menubar and toolbar are displayed.
window.open(
"https://www.google.com/",
"Google",
"resizable,scrollbars,status");
However, this is likely going to be blocked by their popup blocker.
So I like to analyze websites and try to figure out how they work.
I came across this website.
http://us.battle.net/d3/en/forum/
They have a button that minimizes and maximizes divs. AND it remembers if you left it minimized or maximized, regardless if you're logged in or not. (it's browser specific, i.e. it only remembers based upon the brower you used.)
I see they're calling some javascript on this, but how is this done? Using cookies? I doubt it's being done with sessions since when I log in/out it doesn't affect it.
Thanks.
They are using the cookie forumParentToggle. This cookie is created the first time you expand or collapse a div.
For example, if you collapse the "Classes" div, the cookie is created with the value 3354995.
By deleting this cookie, the Classes div will be re-expanded when the page is refreshed. To the browser, it looks like you are visiting for the first time.
This cookie is referenced in the minified version of javascript they are utilizing here.
I'd like to be able to tell when the user leaves the site so I can update a field in my database (particularly, a last_visit field in order to determine unread items for each user). However, I'm not sure how to manage the fact that sometimes, a user opens several tabs of the site, so I can't use onbeforeunload reliably to accomplish this goal.
Ideally, I would be able to update this field only when there is only one open tab of the site.
On the hand, maybe I could get more functionality by simply using a table to record read items for several days and assuming that threads older than that are read by default.
What do you think?
Regards
All I can think of is using either cookies or local storage to update the time at which they're viewing your site on each page load. This way, once they close all the tabs where your website is open, the cookie/local storage entry won't update, and you can access that value later on when they return.
So run this every time the page loads:
window.localStorage.setItem('lastVisit',Date.now);
And to grab it:
var lastVisit = window.localStorage.getItem('lastVisit');
I have created a redirect based on screen width.
The page is https://galleryofjewels.businesscatalyst.com/home
I would like the redirect to only happen once, when the mobile phone first encounters “home” here: https://galleryofjewels.businesscatalyst.com/home
I am redirecting the “home” page to a page called “about-us-mobile” designed for 480 or less screen width using this javascript:
if (screen.width <= 480) {
document.location = "about-us-mobile ";
}
I only have 2 pages that are optimized for the 480 or less and a link “shop”on those two pages which go back to galleryofjewels.businesscatalyst
I want the mobile phone to click the “shop” button to see the full “home” page without being redirected again to “about-us-mobile”.
So I created a page which is a copy of the “home” page and called it “home_full_site_shop”. It has no redirect on it. The “shop” button on the mobile pages links to this page.
If the mobile phone eventually tries to link back to the home page again via the “shop” button on any page of galleryofjewels.businesscatalyst I would like it Not to redirect again to “about-us-mobile”.
In short, I would like the redirect to only happen once, when the mobile phone first encounters the galleryofjewels.businesscatalyst home page
If you're tying yourself to just JavaScript, your best bet is probably to use a GET variable that is set once your site recognizes the dimensions. This variable being set would indicate to each page that the relevant links should go to the mobile-optimized pages (you could dynamically create the links based on the existence [or not] of this GET var). You could actually avoid both redirects this way. Just pass the GET variable around on each click within your site (this can be done by dynamically modifying all page links onload).
If you also have server-side options (PHP maybe), you have a prettier option. You could use a session/cookie to store whether the current user is mobile or not and send the web browser appropriate links after the session is set.
Simple, by setting a (in legal terms: technical) cookie (that contains no unique information).
The cookie-option is pretty cross-browser supported and well-documented (unless users turned them of, which is quite the rage at the moment due to awareness-tools like collusion). In that case there a 'problem' for the user if you do not have some fall-back mechanism or graceful degradation.
Should you also depend on javascript, then there are 2 requirements to your site:cookies and javascript.
So although javascript/cookie route is easy to implement, in the long run it does not hurt to look a little further:
Or you could dive into local web-storage with all it's current quirks
And you could log it on your server with a database based on IP or something like that.
Yet another option would be (if you render the html serverside anyway) to rewrite/change all links (using GET variables) in the document and scripts...
Or .htaccess mod-rewrite..?
I'm on a roll...
You could also use 2 sub-domains: Catch the user at the front-door, redirecting them to your resolution/app-specific subdomain, and do this using the javascript-function history.replace(); This way the user cannot navigate back in the history to the 'front-door', hence he is trapped ultimately to the root of the subdomain.
Tada :P
So the proper long-term solution to this requirement starts with making a choice..
if (window.innerWidth <= 480) {
window.location = "https://galleryofjewels.businesscatalyst.com/home";
}
I have a PHP and HTML based CMS. In each page of the CMS, there is a form with a Javascript-based tabbed dialog that switches between a number of DIVs.
From each page of the CMS, it is possible to open a preview page to see the changes made, and then return to the CMS. When the user returns, I would like the same tab page to be opened as when they left for the preview.
My current approach is to change the value of a cookie ("current_tab") each time the user changes a tab.
onclick='setCookie("current_tab", 5);'
When the tabbed dialog is generated, I check for the cookie and set the appropriate DIV to "display: block".
However, I need the cookie setting to be limited to that specific CMS page, and not all of them. If the user changes to a different page, the tab must not be pre-selected.
My current approach is to create a cookie for each page, e.g. for page ID 10254:
onclick='setCookie("current_tab_10254", 5);'
and, as an attempt of cleaning up, to remove that cookie when the page is rendered. But obviousy, this is not going to clean up every cookie that was set, because the user can choose not to return to the form, or navigate to a different page. I fear clutter through dozens of cookies in the system.
Does anybody have a better idea how to do this?
Try to Limit visibility of cookies by changing "path" parameter of cookie.
If structure of your application doesn't allow this, I'd suggest you not to use such approach, cause cookies are usually sent with every request, including requests to static information (images, css).
Why are you worried about removing the cookies? They will expire and remove themselves.