Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
cookie possibly blocking clipboard API

I have this bit of code below that essentially on click of an a tag copies the text of an input field to the clipboard and then opens a new tab where the user pastes the text.

This all works completely fine UNTIL the cookie is set. I have tried it with testing 10 of the different a tags before the cookie is set.

Once the cookie is set, the new tab will still open, the input field I am copying the text from still updates, but if I paste into notepad or the window that opens, it will be whatever was copied last and not the most up to date. If I remove the cookie and reload everything goes back to how it should function.

So I am not sure why the cookie being set would stop the clipboard from updating.

 //Set Cookie Function
 function setCookie(cname, cvalue, exdays) {
    const d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    let expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

//Get Cookie Function
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
        var end = document.cookie.indexOf(";", begin);
        if (end == -1) {
        end = dc.length;
        }
    }
    // because unescape has been deprecated, replaced with decodeURI
    //return unescape(dc.substring(begin + prefix.length, end));
    return decodeURI(dc.substring(begin + prefix.length, end));
} 

$(document).on('click', '.pop-top ul li a', function() {
    var copiedRole = $(this);
    $('#copyText').val(copiedRole.text());
    navigator.clipboard.writeText($('#copyText').val());
    
    var acknowledgeExists = getCookie("acknowledge");

    if(acknowledgeExists == null) {
        $('#copyMessage, .frosted').addClass('show');
    } else {
        window.open('[redacted]', '_blank');
    }
});

//Create Cookies
$("#acknowledge").on('click', function() {
    $('#copyMessage, .frosted').removeClass('show');
    window.open('[redacted]', '_blank');

    if($('#never').is(':checked')) {
        setCookie("acknowledge", "yes", 1);
    }
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

So it turns out that it was not the cookie, but the window.open firing immediately. MDN docs for the clipboard API note that the tab must be active. So even though my code has the write to clipboard function before the cookie check, it still would fire quick enough to prevent it.

While not an ideal solution wrapping the window.open in the cookie if/else in a setTimeout for 1 second works fine.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!