JavaScript cookie: Cross-origin access advertisement promotion; javascriptcookie

Source: Internet
Author: User

JavaScript cookie: Cross-origin access advertisement promotion; javascriptcookie

In practical applications, the cross-region weather forecast component can be implemented using the above method. Another common method is to display some e-commerce advertisements, this advertisement will scroll through the products you have visited or products promoted to you by Lenovo.

For example, A webpage A displays two types of advertisement:

In an advertisement in the East, all the items shown in the advertisement were visited and related items were pushed.

The advertisement of a certain treasure is basically the same as the presentation method.

When you access a product of a fortune in the East China Region, the information is put into the cookie and displayed based on the product information in the cookie.

The problem arises.

The website where A Web page is located and the website of A fortune in the East region must be two independent domain names. Therefore, the cookies that access A fortune in the East region cannot be obtained because they are different from each other.

It is impossible and inappropriate to present product information on Webpage.

Of course, we need to present product information through cross-origin. The problem to be solved is:

1. The cookie cannot be obtained in the script generated by the cross-origin service. It can only be obtained on the Cross-origin server.

Why ?, The script generated by the cross-origin service will eventually run on webpage A. The cookie accessed by the script generated by the cross-origin service can only be the cookie of the site where webpage A is located, which is incorrect.

2. The cross-origin service background can receive cookies

The answer is yes. As long as the browser initiates a request to a domain name/address, the corresponding cookie will be taken over.

So let's implement a simple demo.

Demo architecture: node. js + express

1. A cross-domain service can be understood as an e-commerce company. It provides a page for inputting product information, simulating accessed items, inputting them, and saving them to cookies.

Page

The Code adds an expiration time to the input and stores it in the cookie. Of course, you can simply compile a code first.

<! DOCTYPE html> Router. get ('/ad', function (req, res) {// concatenate a JS string to complete the html Tag printCookies (req. cookies); var s = 'document. write (\ '<div style = "background-color: red; width: 10rem; height: 10rem"> item ad'; // retrieve all items in the cookie, in the script string for (var p in req. cookies) {s + = '<div>' + unescape (req. cookies [p]) + '</div>';} s + = '</div> \'); '; console. log (s); res. setHeader ('content-type', 'text/plain cirpt; charset = UTF-8 '); res. write (s); res. end () ;}); function printCookies (cookies) {console. log ('******* cookies *******'); for (var p in cookies) {console. log (p + '=' + unescape (cookies [p]);} console. log ('*******************');}

3. Perform A script request for the cross-origin service on Webpage A of the local website.

The script tag references the address of the script provided on the Cross-origin service.

<! DOCTYPE html> 

After the page is running, you can list the commodity information you have accessed, as if you were using a small advertisement.

This is done.

I would like to introduce so much about the advertisement promotion of cross-origin access to JavaScript cookies. I hope it will be helpful to you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.