'Get cookie of parent page (iframe) using JS

I need to get a cookie created in parent page.

enter image description here

I need get a spesific cookie (example) who was cread on modiface--locatelcolomia.myvtex.com and used this on my child page modiface.locatelcolobmia.com (this one is called by vtex iframe)



Solution 1:[1]

Thanks for the help, this is the answer: I got the cookie by Jquery and save on a input type hidden at the end send to my iframe calling a php page and send this value by URL (GET).

jQUERY

let cartId = $('#idCartInput').val();

IFRAME

("#idCartBtn").html("<iframe src='http://localhost:81/projectExample/modiface/index.php?cartId="+cartId+"' class='frameModiface' id='pruebasId' allow='camera' name='iframe_a'></iframe>");

Solution 2:[2]

You cannot directly get cookies from other pages.

You can send a request to that page to send the cookies.

getCookies.php (put on parent page)

window.onload = function(){document.getElementById('submitpost').submit();}
<?php if(!isset($_COOKIE['COOKIE_NAME_HERE']) {die('set cookie');} ?>

<h3>Please wait</h3>

<form id="submitpost" action="PUT_PREVIOUS_PAGE_URL_HERE" method="post">
  <input type="hidden" name="cookie" value="<?php echo $_COOKIE['COOKIE_NAME_HERE']; ?>">
</form>

Then set the cookie to that. Redirect users without the set cookie to the getCookies.php page.

This form can be intercepted and changed, but cookies can as well. Never put account details in cookies.

This answer requires a web server with PHP, chances are that your host does support PHP.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 ethry