In Safari, non-ascii characters are incorrectly stored in document.cookies as trash

Below is a snippet of JavaScript code that I am working with:

wdata['account'] = {"value": $(input).val(), "title": "Номер карты получения"};
var r = {
    "ipayway": ipw_selected,
    "wpayway": wpw_selected,
    "amount_type": amount_type,
    "amount": amount,
    "email": email,
    "idata": idata,
    "wdata": wdata,
    "udata": udata
}
console.log(JSON.stringify(r));
document.cookie = "r=" + JSON.stringify(r) + ";";
console.log(document.cookie);

When debugging in Chrome and Firefox, here are the results:

{"ipayway":"3","wpayway":"2","amount_type":"invoice","amount":"10","email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="90e2f5f6f5e2d0f6f1e3e4f5e2bef3fffd">[email protected]</a>","idata":{"phone":{"value":"79111111111","title":"phone"}},"wdata":{"account":{"value":"4444444444444448","title":"Номер карты получения"}},"udata":{}}

_ga=GA1.2.1726207989.1464355649; _ym_uid=1464355650833173718; _ym_isad=2; r%3Dfalse%3B; r={"ipayway":"3","wpayway":"2","amount_type":"invoice","amount":"10","email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c4b6a1a2a1b684a2a5b7b0a1b6eaa7aba9">[email protected]</a>","idata":{"phone":{"value":"79111111111","title":"phone"}},"wdata":{"account":{"value":"4444444444444448","title":"Номер карты получения"}},"udata":{}}; _gat=1; _ym_visorc_37606275=w

In Safari:

{"ipayway":"3","wpayway":"2","amount_type":"invoice","amount":"10","email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="daa8bfbcbfa89abcbba9aebfa8f4b9b5b7">[email protected]</a>","idata":{"phone":{"value":"79111111111","title":"phone"}},"wdata":{"account":{"value":"4444444444444448","title":"Номер карты получения"}},"udata":{}}
    
r={"ipayway":"3","wpayway":"2","amount_type":"invoice","amount":"10","email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1e6c7b787b6c5e787f6d6a7b6c307d7173">[email protected]</a>","idata":{"phone":{"value":"79111111111","title":"phone"}},"wdata":{"account":{"value":"4444444444444448","title":"A1468837743323372246t%3A1468837754%3A; ga=GA1.2.1861993315.1468837742; gat=1; ym_isad=2; ym_uid=1468837743323372246; _ym_visorc_37606275=w

I'm facing an issue where, in Safari, instead of getting

"title":"Номер карты получения"
as in Chrome or Firefox, I am seeing something like
"title":"A1468837743323372246t%3A1468837754%3A
. Furthermore, in Safari, it appears to cut off the last closed braces compared to the debug results in other browsers.

Answer №1

To ensure proper encoding, it is necessary to include "charset=utf-8" in the HTML Meta Tag and within your JavaScript code.

<meta charset="utf-8">

Also, remember to specify utf-8 in the script tag when linking a JavaScript file:

<script type="text/javascript" charset="utf-8" src="/js/xxx.js"></script>

Answer №2

I came across some valuable information on a post at . In the post, it mentioned that Safari has issues with sending cookies containing non-ASCII characters. This is why when dealing with cross-browsing, it's recommended to use encodeURIComponent.

In my specific situation, my code will look like this:

wdata['account'] = {"value": $(input).val(), "title": encodeURIComponent("Номер карты получения")};
var r = {"ipayway": ipw_selected,
    "wpayway": wpw_selected,
    "amount_type": amount_type,
    "amount": amount,
    "email": email,
    "idata": idata,
    "wdata": wdata,
    "udata": udata
  }
document.cookie = "r=" + JSON.stringify(r) + ";

The value of the cookie's title parameter will be:

%D0%9D%D0%BE%D0%BC%D0%B5%D1%80%20%D0%BA%D0%B0%D1%80%D1%82%D1%8B%20%D0%BF%D0%BE%D0%BB%D1%83%D1%87%D0%B5%D0%BD%D0%B8%D1%8F
, which can be easily processed on the Back-End. For instance, in Python:

import urllib
title = '%D0%9D%D0%BE%D0%BC%D0%B5%D1%80%20%D0%BA%D0%B0%D1%80%D1%82%D1%8B%20%D0%BF%D0%BE%D0%BB%D1%83%D1%87%D0%B5%D0%D0%BD%D0%B8%D1%8F'
>>> print urllib.unquote(a)
Номер карты получения

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Dynamic Reading of JQGrid Colmodel and ColNames from ASPX CodeBehind

We're in the process of upgrading our grid system to JQGrid. Our goal is to dynamically bind the grid from the code behind, including the ColModel and ColNames. The column names and model will be determined at runtime. We've attempted the follo ...

Unexpected token error in TypeScript: Syntax mistake spotted in code

Being new to Angular, I understand that mastering TypeScript is crucial for becoming a skilled Angular developer. Therefore, I created this simple program: function loge(messag){ console.log(messag); } var message:string; message = "Hi"; loge(messa ...

The `chrome.windows` API is not defined for this particular Chrome

I'm currently in the process of developing a Chrome extension that will allow users to effectively manage open tabs within a specific application's website. Here is the current manifest file: { "manifest_version": 2, "name": "AT Tabs", ...

Guide to accessing a nested and potentially optional object property with a default value and specifying its data type

Just a simple query here... my goal is to extract data.user.roles, but there's a possibility that data may be empty. In such cases, I want an empty array as the output. Additionally, I need to specify the type of user - which in this instance is any. ...

Ways to eliminate the dotted line from the image map in Internet Explorer 11

Below you will find the code I am working with: <img alt="Testing 2015" border="0" src="images/Test-2015.jpg" usemap="#Map" /> <p><map name="Map"><area coords="790,100,653,135" href="http://www.google.com/" shape="rect" style="cursor ...

Tips for importing a .ts file into another .ts file within an Angular 5 application

I have a bunch of utility methods stored in a file called utils.ts that I want to reuse across multiple components. I'm not sure if it's even possible, and if it is, where should I place the import statement and what would be the correct syntax. ...

The question of when to utilize userEvent.click versus fireEvent in React Testing Library

I'm currently diving into the world of React-Testing-Library. One question that I have is regarding testing mouse interaction on an element. I find myself a bit confused about whether to use userEvent.click(element) or fireEvent.click(element). Can b ...

Incorporating an array of JSON into a Mongoose schema in JavaScript

I am currently developing an Android App focused on baseball, and I have decided to use MongoDB to store my data. The format in which I would like my JSON data stored in the database is as follows: {"<a href="/cdn-cgi/l/email-protection" class="__cf_em ...

"Navigate through the page by scrolling with your mouse all the way to 100

Is it possible to trigger an action when a user starts scrolling using the mousewheel? I've searched online but all I found was information on 100% scroll on click. I want the window to automatically be scrolled down to 100% as soon as the user starts ...

What is the best way to perform an AJAX request in Typescript with JSON data?

Currently, I am delving into the realm of AJAX and encountering some hurdles when attempting to execute an AJAX request with parameters. Specifically, I am facing difficulties in sending JSON data: My approach involves utilizing Typescript in tandem with ...

Retrieve the JSON value from the remoteMessage received within the onMessageReceived function for Firebase Cloud Messaging push notifications

How can I access the value of image_id within the JSON object payload, which is nested in the JSON object data retrieved from the remoteMessage in the onMessageReceived method? MyFirebaseMessagingService.java public class MyFirebaseMessagingService exten ...

Template7 produces a bizarre outcome referred to as "each this" whenever JSON is utilized

Currently, I am experimenting with dynamically loading content on Framework7/Template7 pages. Everything works perfectly fine when using static "context" in JSON format. However, when attempting to test it with real-world data from an API, I encounter a st ...

How to retrieve an array value within a function in AngularJS

<select class="form-control" id="column" ng-model="selectedcolumn" ng-options="column for column in columns"></select> <input type="text" ng-model="test[selectedcolumn]" ng-change="search()" /> Is there a way to retrieve the value o ...

multer - the file uploaded by the request is not defined

I've been working on an app with Node, Express, and multer for image uploads. However, after submitting the form, req.file is coming up as undefined. I've spent hours trying to troubleshoot this issue but haven't been able to pinpoint the pr ...

Unable to construct Vue application after integrating webpack extension

Currently facing an issue with adding a webpack extension to build my Vue app. Despite having Vue/cli which includes webpack, I have also attempted to install different versions of webpack without success. Anyone experienced the same problem and found a so ...

Ways to disable mousewheel function in jQuery

I am trying to deactivate the mouse scroll in jQuery and successfully did so, however, I encountered this error message jquery.min.js:2 [Intervention] Unable to preventDefault inside passive event listener due to target being treated as passive. See Thi ...

Develop an innovative showcase page showcasing 151 individual profiles with React Router

I am new to using React Router and feeling a bit confused about how to proceed. On my homepage, I have 151 unique monster thumbnails. When a user clicks on a thumbnail, they should be directed to the specific monster's 'show page'. Currently ...

What is the best way to design a timetable schema in MongoDB specifically for a Teacher Schema?

Greetings to all in the StackOverflow community! I am here seeking some innovative ideas for a project I am currently working on. One of the challenges I'm facing involves storing available days within a Teacher Schema. In this application, a Teacher ...

What is the best way to gather user input and incorporate it into a selected template, ensuring it is verified before sending?

I'm in the process of developing a project that involves gathering user input through a collector and displaying it on my template for verification before sending it out. The format I'm aiming for can be seen here: This is the template format I ...

cracking reCAPTCHA without needing to click a button or submit a form (utilizing callback functions)

Currently, I am facing a challenge with solving a reCaptcha on a specific website that I am trying to extract data from. Typically, the process involves locating the captcha inside a form, sending the captcha data to a captcha-solving API (I'm using ...