0

This code replaces the image And keeping the cookies

Is there a mistake? I do not know the solution.

<html>

<button onclick="hideImage()"> Remove img </button>
<img id="my_images" src="http://karachiairport.com.pk/images/data-section/airline/24-11.png">
<script>

document.addEventListener('DOMContentLoaded', function() {
    // If cookie exist
    if (document.cookie.indexOf('image_clicked') > -1) {
        document.getElementById("my_images").src="http://www.1dmag.com/all_asset/img/no-image.png";
    }
 

    function hideImage() {
        document.getElementById("my_images").src="http://www.1dmag.com/all_asset/img/no-image.png";
        document.cookie = "image_clicked=true";
    }
});


</script>
DeiDei
  • 10,205
  • 6
  • 55
  • 80
  • Please check the following link, Hope it may help you [Link](https://stackoverflow.com/questions/30183320/iframe-sandboxing-with-allow-same-origin-flag-error) – vijesh May 17 '18 at 06:31

1 Answers1

0

Please try this.

Now 'hideImage is not defined' error is resolved.

<html>

<button onclick="hideImage()"> Remove img </button>
<img id="my_images" src="http://karachiairport.com.pk/images/data-section/airline/24-11.png">
<script>

document.addEventListener('DOMContentLoaded', function() {
    // If cookie exist
    if (document.cookie.indexOf('image_clicked') > -1) {
        document.getElementById("my_images").src="http://www.1dmag.com/all_asset/img/no-image.png";
    }
 

   
});

 function hideImage() {
        document.getElementById("my_images").src="http://www.1dmag.com/all_asset/img/no-image.png";
        document.cookie = "image_clicked=true";
    }
</script>
Amar Kaygude
  • 247
  • 3
  • 6
  • when i refresh the page the first img appear again, I dont need reload it – Mosaab Ahmad May 17 '18 at 07:50
  • On which browser you are running this code? Chrome ignores cookie from local pages. I served this code from lite-server, it is working as per your expectations. Please check this link. [https://stackoverflow.com/questions/8105135/cannot-set-cookies-in-javascript] – Amar Kaygude May 17 '18 at 09:06