Leaflet attribution link should have a target of "_blank"

Created on 3 Feb 2012  路  6Comments  路  Source: Leaflet/Leaflet

Hi,
The default Leaflet attribution add a link at the bottom-right of the map. If you click this link, it changes the current page and send the user to the leaflet webpage.
It is a bit annoying when you mis-click or when you touch it by accident because you lose the whole context (bounds) of your page. Setting the default link to target="_blank" should do the trick

Fabien

feature

Most helpful comment

@fnicollet you can change the prefix on the fly like this:

map.attributionControl.setPrefix('your new powered by prefix with target=blank');

I'll close this for now as I agree the best practice is not to force opening new windows (unless you're embedding it in an iframe).

All 6 comments

See: http://www.thatwebguyblog.com/post/dont_use_target_blank/

The target attribute has been deprecated for this reason. There are scripting workarounds, but it's considered a usability mistake to force new windows/tabs to open.

Hello Majiir,
I've never read that blogpost before and i find it quite narrow-minded. I know that if you middle click, you get any link in a new tab.
My use case is this. I'm using an iPad, panning around and i mis-touch the Leaflet link. It takes me to a new webpage and i've lost my work / context. Now this is a real usability issue. If the link wasn't right in front of the map, i would agree but you can easily click/tap it by accident.
Plus i don't even know if it is possible on an iPad to open link in a new tab voluntarily. I tried a long tap on it and nothing came up so i guess there is none.

I think it would benefit any leaflet user / dev.

Fabien

I am not familiar with Leaflet's behavior on the iPad. However, on my Android device, the attribution control is very small and difficult to accidentally tap. I suggest you try one (or both) of these ideas:

  • Modify your CSS to reduce the size of the control on the iPad. There are Apple-specific CSS properties to help with this. (See here and here).
  • Modify the attribution links so they open new windows. I highly recommend using JavaScript to accomplish this instead of setting the target attribute, which is deprecated in some HTML standards.

ok, well, maybe the correction would to at least give the a element an id so that it can easily be fetched and modified

Fabien

This CSS selector (also usable with jQuery) should do the trick: .leaflet-control.attribution a

If you don't want to use jQuery, you can use getContainer() on the attribution control object and find the anchor tags from there.

@fnicollet you can change the prefix on the fly like this:

map.attributionControl.setPrefix('your new powered by prefix with target=blank');

I'll close this for now as I agree the best practice is not to force opening new windows (unless you're embedding it in an iframe).

Was this page helpful?
0 / 5 - 0 ratings