Search code examples
javascriptgoogle-chromefirefox

How to avoid a bookmarklet opening an extra, blank tab?


Following directions found here, I built a bookmarklet that would open a date-dependent site. The bookmarklet is one in a bookmark folder I will click on to have each of the folder's bookmarks open in a new window. If I click on the bookmarklet in an already open window, I get just an added tab it the expected url. If instead I click on the parent folder I get the parent's bookmarks plus a blank tab. My guess is that this is an unavoidable "feature", but if there's a way to avoid the blank I'd like to learn it.

Here's the javascript:

javascript: (() => {   const det = "https://comicskingdom.com/candorville/"; const today = new Date();const year = today.getFullYear();const month = String(today.getMonth() + 1).padStart(2, '0');const day = String(today.getDate()).padStart(2, '0');const formattedDate = `${year}-${month}-${day}`;window.open(det+formattedDate);   })();

Solution

  • window.open

    1. The syntax is

      window.open(url,name,parameters)
      
    2. The user has to grant the script the right to pop the window.

    So try

    window.open(det+formattedDate,'_blank','width=500,height=500')
    

    or even this one I haven't tested or seen before

    window.open(det+formattedDate,'_blank','popup')
    

    and note the location bar might give you an icon where you can accept the script wants to pop a new window

    It seems _self will work, at least in Chrome

    window.open(det+formattedDate,'_self')