1

Using object.on('selected', function() {}), I can get the image object that is clicked. My intent is to redirect to an URL by clicking it. How can I add an URL to an image, or more general, to an object in Fabric.js?

Henry
  • 1,077
  • 1
  • 16
  • 41

1 Answers1

2

You can simply navigate to the target page in the selected event handler:

object.on('selected', function() {
    window.location.href = "http://www.example.com/";
})
AlliterativeAlice
  • 11,841
  • 9
  • 52
  • 69
  • I want to save the URL in the object. – Henry Apr 12 '15 at 03:44
  • 1
    @Henry What do you mean by save? You can just do `object.associatedUrl = 'http://www.example.com/'` and then in the selected handler do `window.location.href = this.associatedUrl;` if that's what you mean. – AlliterativeAlice Apr 12 '15 at 06:12
  • Do you want to save the canvas on database? – ptCoder Apr 13 '15 at 09:26
  • This works well, thank you! Two things that helped me implement: (1) the object has to be selectable, so if you have `journalName.selectable = false` then it won't work, obviously, and (2) it has to go before you `canvas.add(object)`. – Ari B. Friedman Dec 21 '16 at 01:24