jquery cookie , still set after clear browser history - javascript

I'm using a simple script to show a popup with a checkbox to not show it again, and it works just fine, and it works perfectly to delete the cookie via the script.
My problem is that if i delete the cookies in IE11 (company browser), the cookie is still set!
So what am i missing? Is the cookie stored somewhere other than the standard directory when using jquery.cookie.js ?
I want to be able to see the popup by delete the cookie locally.
My code for reference:
<script type="text/javascript">
$("#Cookiehandler").change(function() {
if(this.checked) {
$.cookie('the_cookie', 'no');
}
});
window.jQuery(document).ready(function() {
if($.cookie('the_cookie') == null) {
$.fancybox.open('#popup_box');
}
else {
// placeholder
}
});
//$.removeCookie("the_cookie");
//alert( $.cookie("the_cookie") );
</script>
Best regards,
Marcus

I think the best way might be for you to have an administrative page with a script that deletes the cookie and just to visit that page whenever you want the cookie to be killed. That's a browser-independent method and you don't have to worry about where the cookie was stored, nor do you have to worry about doing it from a bunch of different browsers manually. Just visit the admin page from some or all of your browsers and it's done...for those browsers...while leaving the other ones alone unless you browse the kill page from them too.

Related

When does PageTransitionEvent.persisted evaluate to true?

I'm trying to detect if my current page is loaded from cache or is a fresh copy.
I have the onPageShow callback registered on my body tag.
I can see it being triggered, but I cannot produce a circumstance where the event.persisted is actually true.
I've even put firefox in offline mode and I see the response being fetched from cache on the network tab but event.persisted is still false.
Umm I can confirm var isCached = performance.getEntriesByType("navigation")[0].transferSize === 0; this does work on Chrome. Worth trying out.
Also as other suggested you might wanna look at this example How can I use JavaScript to detect if I am on a cached page
From google books
This works in mozilla perfectly.
Try the below code
<meta http-equiv="Cache-control" content="public">
...
<body onpageshow="onShow(event)" onpagehide="onHide(event)">
<div >
<a href='/new.html' >Next page</a>
</div>
<script>
function onShow(event) {
if (event.persisted) {
alert('Persisted...');
}
}
function onHide(event) {
if(event.persisted) {
alert("Persisted")
}
}
</script>
</body>
Add any code in new.html. Blank page is also fine
Then use the browser back. You'll get the alert persisted
Note:
Use a domain or ngrok . Cache doesn't work in local
Reload wont trigger persisted. I tried only with page show/hide
I'am skipping the alternative answers to find cache or not
IE11 does have window.performance.getEntriesByType('navigation') but doesn't have transferSize. However, it seems to leave out connectEnd if the page comes from browser cache.
Extending on #subhendu-kundu s answer, this should also work on IE11
<script>
window.addEventListener('pageshow', function(event) {
if (window.performance) {
var navEntries = window.performance.getEntriesByType('navigation');
if (navEntries.length > 0 && typeof navEntries[0].transferSize !== 'undefined') {
if (navEntries[0].transferSize === 0) {
// From cache
}
} else if (navEntries.length > 0) {
// IE11 seems to leave this completely if loaded from bfCache
if (!navEntries[0].connectEnd) {
// From cache
}
}
}
});
</script>
I don't know if i understood your question correctly, you want to check if the page that is loaded is from disk/memory cache or a fresh one. Please comment below if i understood it wrong.
I'm trying to detect if my current page is loaded from cache or is a
fresh copy.
For this you can open the developer tools of your browser and check the network tab, if the page is loaded from cache it will show indication (from cache).
Chrome supports this out of the box but for fire fox i think you should install web-developer plugin : https://addons.mozilla.org/en-US/firefox/addon/web-developer/
Well one thing I can suggest to disable the cache in the browser and check the size of the fetched code chunk. For the same you can disable the cache from browser itself..(I am just suggesting my views).

Detect when a user leaves a page and write a cookie

I am implementing a cookie banner and need to have inferred consent once the user has seen the banner. I am therefore trying to write a cookie once the user starts to navigate the site to say they've seen the cookie banner. I've tried using beforeunload and unload, but neither seem to do anything.
The latest bit of code I've used this is:
<script>
$(window).unload(function () {
jQuery.post("/set-cookie-preference", { cookiePreference: 3 },
function (data) {
//document.location.reload();
});
});
</script>
I also tried:
<script type="text/javascript">
function closeIt() {
jQuery.post("/set-cookie-preference", { cookiePreference: 3 },
function (data) {
//document.location.reload();
});
}
window.onbeforeunload = closeIt;
</script>
But neither seem to work. Any ideas?
You could use LocalStorage. It works the same like a cookie, but it has some more benefits, in my opinion. A LocaStorage object can only be removed by the user (like when we wipes his browser history). To set a normal PHP cookie you have to set a time.
You can read more about it here:
http://www.w3schools.com/html/html5_webstorage.asp
All major browsers (Chrome, IE, FireFox, Safari) support LocalStorage. Even IE 8 supports LocalStorage!

Jquery Cookie : one time use cookie

I want to put cookie to my page.
Here's the logic :
When I load the page, I want it to load a popup or bootstrap modal. But the modal only load once when the browser is active. And only will load again when the browser tab is closed or exits the browser application. I have used session to do this, but I prefer to use cookie for personal preferences.
Is there a way to do this with javascript?
I've tried with $(window).load(), and $(window).on('beforeunload',function());
Javascript :
<script type="text/javascript">
$(window).load(function () {
if( $.cookie('firstLoad') == 'unloaded' || $.cookie('firstLoad') == 'null' || $.cookie('firstLoad') == null ) {
$('#openLoading').modal('show');
var time_exp = 1;
$.cookie('firstLoad','loaded',{ expires: time_exp });
}
});
$(window).on('beforeunload', function (){
alert($.cookie('firstLoad'));
$.cookie('firstLoad','unloaded');
});
</script>
The problem is sometimes the app browser will execute location.reload() and will reset the cookie in some way and make the popup appear again.
Please provide solution, thanks.
PS : the var time_exp and expires : time_exp is a last resort if the unload doesn't work
The beforeunload event doesn't just fire when the tab is closed. It will fire whenever the user goes to a new page in the same tab, including a page on your site. So you are resetting your cookie every time the user navigates between pages.
There is no event you can use to tell you the user is leaving your site specifically. The closest you can do is not set an expires, so that the cookie will automatically be deleted when the browser exits.
You could put a close button in the modal, and set a cookie when it is clicked so you know the user has viewed the modal and you don't need to show it again for however long you decide.

run javascript function by url hash

I have a website which has all pages contents in one page called "pagecontents". When a menu in navbar is clicked the jQuery load function is triggered and the relevant content is loaded into the main page.
It's working fine, however I added an hash to the URL for whenever a particular content is loaded , so that users can go directly into viewing relevant content when they type or paste the url with the hash. This works fine in my local host but not on remote host I wonder What's the problem.
if(location.hash == '#web') {
$('#contentFetch').load('pagecontents.php #webC');
}if(location.hash == '#graphic') {
$('#contentFetch').load('pagecontents.php #graphicC');
}if(location.hash == '#mobile') {
//$('#testLocation').text("mobile Works");
$('#contentFetch').load('pagecontents.php #mobileC');
}if(location.hash == '#contact') {
$('#contentFetch').load('pagecontents.php #contactC');
}else{
$('#contentFetch').load('pagecontents.php #indexC');
}
Edit: About the hash in query string:
the website url ex: 'www.mywebsite.com/'. when i add the '#graphic' at the end: 'www.mywebsite.com/#graphic' it's not loading the content. I noticed that if keep on pressing enter even though is not loading, it then loads!!!? it seems very incosistent as it loads 1 time out of 5. I wonder if using this is reliable in real world, or is there another way of doing it? Thanks, Mike
Make sure to check the runtime versions that your host is using and that they comply with the ones you have locally.
Usually, a host will run a version of a software that is often outdated in order to accommodate older websites. (ex. running PHP 4 instead of PHP 5).

Chrome not supporting my javascript

I just want, when a user comes to my site and closes the window, then I want to know the reason for leaving from my site.
So I'm sending the user to a survey page, using the following script.
It works in every browser but not in Chrome
<script type="text/javascript">
window.onbeforeunload = confirmExit;
function confirmExit() {
if (location.href.indexOf("index.php") != -1)
{
location.href = "http://www.test.com/survey.php";
return "Press 'cancel to go 'survey'";
}
}
</script>
Have you tried to use window.location = ("http://www.test.com/survey.php"); or window.open ("http://www.test.com/survey.php"); instead? That may be easier to do.
You can also do window.open ("http://www.test.com/survey.php", '_newtab'); This will make users less upset off when they are forced to redirect after leaving, because it will be in a new tab.
Well, sometimes there is a good reason for such a code user310850 is quoting
not all websites are in Web, some of them are internal corporate websites
Some if not most of big companies still use IE 6 as standard browser
I would use unload event handler. I assume jquery is good
$(window).unload(function() {
//your code here
});

Categories