1

Is it possible to call the Ctrl+D shortcut using JavaScript button click?

I want to add a "Add To Bookmark" button on my custom context menu for my website, for that I want a function that calls the "Ctrl+D" shortcut on a button click to show the browser's default "Add to Bookmark/ Favourites" dialog box.

I have tried many pre available solutions from internet but none of them is working for me.

let bookmark = document.querySelector('.bookmark');

bookmark.addEventListener('click', ()=> {
  // Call The "Ctrl+D" Shortcut!
});
<button type="button" class="bookmark">Bookmark</button>
Anuj Thapa
  • 55
  • 5
  • Does this answer your question? [How do I add an "Add to Favorites" button or link on my website?](https://stackoverflow.com/questions/10033215/how-do-i-add-an-add-to-favorites-button-or-link-on-my-website) – Christophe Quintard Aug 27 '22 at 07:14
  • I'm fairly sure that you can't do this, and with good reason: your JavaScript code isn't supposed to hook into methods of the browser itself unless they are explicitly exposed (i.e. print). If the browser doesn't offer programmatic access to bookmarks, this can't be done. `Ctrl+D` isn't an aspect of a webpage itself but of the containing application. – filipe Aug 27 '22 at 07:14

1 Answers1

0

You can simulate keypress events using JavaScript.. but I'm not sure this will work as per your need

Give it a try

window.dispatchEvent(new KeyboardEvent('keydown', {
        key: "d",
        ctrlKey: true,
        bubbles: false,
        metaKey: false   
    })
);

For more details visit here

Ramkumar G
  • 415
  • 1
  • 8