I have been attempting to write code to dynamically change a particular href value to the current url. It works to a point, but the problem is that on my page I have a few different filters, so when one is clicked, the current url is then overridden.
is there a way to grab the first url as a variable when the page loads and keep it the same until someone leaves the page ?
I've been experimenting with various conditional statements but not had much luck.
The code that works is this one :
$(document).ready(function() {
var currenturl = document.URL;
$("#reset").attr("href", currenturl);
});
Thanks in advance!
You could store the initial url as a cookie.
$(document).ready(function() {
function setCookie(cname, cvalue) {
document.cookie = cname + "=" + cvalue + ";path=/";
}
function getCookie(cname) {
const name = cname + "=";
const ca = document.cookie.split(';');
for(let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
setCookies('INITIAL_URL', document.URL);
$("#reset").attr("href", getCookie('INITIAL_URL'));
});
And then use some logic when you need to reset the cookie value.
Related
I have an error getting cookies from browser.
I have the following javascript cookie.
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
On a different server it works like a charm, on a new server I do not receive back cookie's value. The cookie is created on same domain.
Edit:
I'm expecting to get cookie's value through this script but I am not getting it.
Is there an exception if cookie is created for www.domain.com should not be available domain.com?
I also have to mention that there is exactly the same file on both servers.
I have two variables which needs to be created as a cookie. Can I give them just as without giving any expiration date but just as a key value pair,
document.cookie = "<%= this.CookieDFKey %> = id";
alert (document.cookie);
document.cookie = "<%= this.CookieDateCompleteEnd %> = lastRunDate";
window.location = '<%= ResolveUrl("~/GUI/DRNEW.aspx") %>';
When I gave the alert statement to check what value it is having it shows me
I need to have both the values id and lastRunDate avaiable in the called page. Can I be just using Request.Cookie[the name of cookie where the value store]?
First cookies are key value pairs, you will get all cookies in Request.Cookies
If i'm not wrong in C#
if (Request.Cookies["UserSettings"] != null)
{
string userSettings;
if (Request.Cookies["UserSettings"]["Font"] != null)
{ userSettings = Request.Cookies["UserSettings"]["Font"]; }
}
Read the below url to set multiple cookies in document.cookie
Setting multiple cookies in Javascript
document.cookie = "id=<%= this.CookieDFKey %>";
document.cookie = "lastRunDate=<%= this.CookieDateCompleteEnd %> ";
To retrieve cookie use the following code
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length,c.length);
}
}
return "";
}
Hi I am trying to get token value from cookie and add it to the URL but extra characters like *(asterisk) are being added. please help me how should i pass my token value in URL without adding any characters.
here is my javascript code
urls.put("AppName", "javascript:submitApplication()");
function submitApplication() {
var cookieToken = "SSOCookie";
var newToken = GetCookie(cookieToken);
myWindow = window.open(myhost + "/webapp/sso?ssotype=type1&appname=APP1&mode=ARD&newToken="+newToken,'ApplicationName');
myWindow.focus();
}
My newToken value is added with stars as below
https://test.test1.com/webapp/sso?ssotype=type1&appname=APP1&mode=ARD&newToken=AQIC5wM2LY4SfczjfZ-64LqYUNHjjYVmu13iznLg1gKZoas.*AAJTSQACMDIAAlNLABM2MTI2ODc1MDg4MDc4MzE0NjI0AAJTMQACMDY.**
But i want this to be passed as ( without adding any characters)
https://test.test1.com/webapp/sso?ssotype=type1&appname=APP1&mode=ARD&newToken=AQIC5wM2LY4SfczjfZ-64LqYUNHjjYVmu13iznLg1gKZoas.AAJTSQACMDIAAlNLABM2MTI2ODc1MDg4MDc4MzE0NjI0AAJTMQACMDY.
Please help me how can i pass this token value in URL without allowing any extra characters to be added . Thank you
I would do something like this:
Make a function that accepts a cookie name, to retrieve the cookie you're looking for.
function getCookie(cookieName) {
var name = cookieName + '=';
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return '';
}
Then whenever you need to get that cookie, you just need to declare a variable and call that function.
var myCookie = getCookie('name_of_the_cookie');
Add the cookie to the url.
var basicUrl = 'https://www.google.dk';
var fullUrl = basicUrl + '/' + myCookie;
Desperately reaching out here now!
My webpage contains three columns with three different divs: "primary", "main" and "secondary".
I would like to maintain the scrollpositions of all divs on reload.
For the the first column "primary" I'm using this script below, that works lika a charm.
I know getElementbyID only can get one div, so how could I possibly do?
window.onload = function() {
var strCook = document.cookie;
if (strCook.indexOf("!~") != 0) {
var intS = strCook.indexOf("!~");
var intE = strCook.indexOf("~!");
var strPos = strCook.substring(intS + 2, intE);
document.getElementById("primary").scrollTop = strPos;
}
}
function SetDivPosition() {
var intY = document.getElementById("primary").scrollTop;
document.cookie = "yPos=!~" + intY + "~!";
}
The way that you set the cookie seems to be set up primarily for one item - I would change this so that you can set cookies by a key value pairing.
Using some functions for getting and setting cookies, I would then change your code to:
//add cookie functions
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length,c.length);
}
}
return "";
}
//I am assuming you will have some sort of on scroll function for setting your cookie. Change this to a function so you can bind it multiple times
function bindScrollEvent(elementId) {
var element = document.getElementById(elementId);
element.onscroll = function() {
setCookie(elementId, element.scrollTop); // use the element id to set the cookie to the scrolltop value
};
// once the event is bound, you can set it's current scroll position
var scrollTopPosition = getCookie(elementId);
if (scrollTopPosition !== '') {
element.scrollTop = scrollTopPosition;
}
}
// reload the div positions when the document loads
window.onload = function() {
bindScrollEvent('primary');
bindScrollEvent('secondary');
bindScrollEvent('main');
}
I have searched several places trying to find out how to do display only the value of a cookie rather than the whole key but they all seemed needlessly complex for what I'm doing. I've got a single cookie with only one key, userName = something, and I can't figure out how to display only the "something" rather than userName = something.
function userCookie(form)
{
if(form.User_Name.value == "")
{
alert("Cannot accept a blank user name, please enter a valid name");
return false;
}
else
{
document.cookie="userName=" + form.User_Name.value;
alert(document.cookie);
return false;
}
}
function newWindow()
{
var userWindow = window.open("","MyUserName","height=300,width=300");
userWindow.document.open();
userWindow.document.write("<p>Welcome Back</p>");
userWindow.document.write(document.cookie);
userWindow.document.close();
}
If you always have exactly one cookie then document.cookie.split("=")[1] would be simple enough.
document.cookie will always return key,value pair like cookie1=value1;cookie2=value2.
You can use the following function to split the cookieName and "=" from the document.cookie to get the value.
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) != -1) return c.substring(name.length,c.length);
}
return "";
}
The code is from w3schools.
var cookieArray = document.cookie.split("; "),
cookieObject = {},
i, pair;
for (i = cookieArray.length - 1; i >= 0; i--) {
pair = cookieArray[i].split("=");
cookieObject[pair[0]] = pair[1];
}
alert(cookieObject.userName);
function getCookie(name) {
let x = document.cookie.split(";").find(a => a.includes(name + "="));
return !!x ? x.trim().substr(name.length+1) : ""
}