ngCookies doesnt work properly after i refresh the page - javascript

Im using angularjs in my project , and when the client log in , than a service save his information in the cookies .
by using this code:
$cookies.put('username',$rootScope.username);
or
$cookies.username = $rootScope.username;
i try them both.
in the first try , i tried : console.log($cookies.get('username'); and it doesnt work at all.
but in the second try when i tried : console.log($cookies.username); it did print the information.
but after i refreshed the page , this $cookies.username; didnt work anymore..
what maybe the problem?

To access the cookie you need to do $cookies.get('username')
It seems in your first console.log there is a typo, you're missing the last parenthesis
Try this
console.log($cookies.get('username'));
Also, make sure your $rootScope.username attribute has a value in it

Related

Get jquery cookie using angular

I'm using jquery.cookie v1.4.1 to set a cookie like this
$.cookie('userCookie', $("#user").val());
Where $("#user").val() is returning something like 'username'
Then in an angular app, am trying to retrieve this cookie using
var userCookie = $cookies.get('userCookie');
But is not working, I'm getting:
var userCookie = undefined
I'm using AngularJS v1.4.8 with corresponding ngCookies
Any help would be nice....
Thanks to charlietfl I got it to work.
It happens that I was setting the cookie for a specific path, let say '/domain/somepath' and then I was trying to retrieve it from '/domain/someotherpath', where the cookie was not available.
I fix it specifying the path on the cookie to all my domain like this:
$.cookie('userCookie',$("#user").val(), { path: '/' });
Why don't you use angular to store your cookies?
$cookies.put('userCookie', $("#user").val());
Btw: its horrible if you use JQuery like this inside your Angular App.
$cookies.get('userCookie');
Your problem maybe: the cookie will be created BEFORE your DOM-Element is even rendered on the page. Try $.cookie('userCookie', 'test123'); , this should work pretty sure (with angular 100%).
How to fix this? First of all, DONT USE JQUERY LIKE THIS IF YOU HAVE ANGULAR!!!
<input ng-model="username"></input>
$cookies.put('userCookie', $scope.username);

Can't seem to retrieve session value or control value on .aspx page javascript function

Wonder if anyone can help as im really stuck on a problem i'm having
Using vb.net I have a web application with several .aspx pages.
on one of my aspx pages I have a javascript function that i want to retrieve a pdf path from a session value i set within code behind. After searching the web everywhere seems to say use something like the following
var pdf_link = '<%=Session["pdfpath"].ToString() %>';
here is the javascript function:
function showmodal() {
var pdf_link = '<%=Session["pdfpath"].ToString() %>';
var iframe = '<div class="iframe-container"><iframe src="' + pdf_link + '"></iframe></div>'
$.createModal({
title: 'PDF Preview',
message: iframe,
closeButton: true,
scrollable: false
});
}
to get the value but i get an error message before i can even compile 'Identifier Expected' and it highlights between the [" as being the problem area. i just can't get this to work what am i doing wrong.
Also because i couldn't workout what was wrong with the above i have tried saving the value i want into a hidden field and tried to access that using the following
document.getElementById("PdfPathHiddenField").value
but that just returns null all the time. I just can't seem to access anything within my javascript function.
What i also see as odd is for instance when debugging i have say page1.aspx and page2.aspx. From page1.aspx in code behind on a button click i use
Response.Redirect("~/page2.aspx")
When I have stopped the debugger within my javascript function on page2.aspx to retrieve the value i notice that the tab with the code where the debugger has stopped for page2.aspx is titled page1[Dynamic] but it is the correct source from page2.aspx not page1.aspx really confused as shouldn't it say page2[Dynamic] on that - is this why i maybe can't seem to access any controls as it thinks it's elsewhere ???? Help
Thanks in advance any help would be much appreciated.
In VB.NET arrays have parentessis, Session("pdfpath").ToString(), in C# or javascript you can use square brackets, but not in VB.
PS: Remeber to mark this an answer if it's a solution for your problem

On page load redirect to the same page with querystring

Hi so i am getting a cookie passed in from another website which brings in some data i need to put in the query string.. i know how to put it in the query string that's not my questions.
My question is what jquery function can i use onpage load which will redirect to the query string.. now i don't want to page to load twice i want this to happen and look like it has only loaded once. I have tried to use a .one jquery but that's not needed anymore as the if statement will validate if the query string needs to change. Also that function is not working correctly anyway.
$(document).one('load', function() {
if (window.location.pathname == '/items.aspx'){
window.location.replace("items.aspx?item1=a80af972-4f78-de11");
}
});
any ideas would be great.
Thanks
You could use window.location.href="items.aspx?item1=a80af972-4f78-de11"but that will also reload the page. Depending on your users internet connection it could seem like one page load.
Another thing worth noting: You are waiting for $(document).on("load") which gets fired when the page is loaded which is exactly what you don't want.
Try this for speed increase:
$(document).ready(function() {
if (window.location.pathname == '/items.aspx'){
window.location.href = "items.aspx?item1=a80af972-4f78-de11";
}
});
Still - this is a thing to do on the server side. Something in the backend like "if item1 is undefined load item a80af972-4f78-de11" would be best.

Fiddler: Is it possible to replace part of URL using OnBeforeResponse function?

if (oSession.HostnameIs("www.youtube.com") && oSession.oResponse.headers.ExistsAndContains("Content-Type","text/html")){
oSession.utilDecodeResponse();
oSession.utilReplaceInResponse("old string","new string");
}
Please tell me if I'm using the above script correctly or not.
Basically, How do I to replace/hide the word dolphin from the search query ? I don't want the client browser(my Google Chrome) to see it by any means.
Example : http://www.youtube.com/results?search_query=dolphin&page=3.
If this is not possible with Fiddler,then what other application do you recommend?
Thank you
You can replace anything in the url inside OnBeforeResponse, but doing so won't do anything useful, because the URL has already been sent to the server by then, so changing it that late has no visible impact to anything outside of Fiddler.
If you want to change the URL, do so inside OnBeforeRequest. In your FiddlerScript, look for the urlreplace handler to see how that works.

javascript cookie, Ember

I want to put a user input into a cookie to save the value for later display. It seems I can write into the cookie, but not able to get the value back. I have this simple test set up:
var test = "TEST";
document.cookie = "usr=TEST;";
$.cookie("usr", test);
Ember.$.cookie('usr', test);
console.log("COOKIE", $.cookie("usr"), Ember.$.cookie('usr'), "cookie.length=", document.cookie.length);
Which results to the output "COOKIE undefined undefined cookie.length=0".
I don't see why I don't get my value out, seems like a simple thing. Any help is much appreciated.
I found the problem - needed to run the project from localhost to test.

Categories