Widget Javascript code (ajax)

The problem is that this only works when the widget is present in a sidebar.

If you need some code to run independently from widget, I reason it makes sense to add that code separately from widget class.

And I also need it to work when I call this widget with the_widget() function, for eg. inside a page. I don’t know how could I retrieve the widget options to pass them in the javascript code…

That function takes instance array with widget data. I am not sure about your specifics, but I thinks you can pass whatever you need in there.

Directly called widget has no data, other than passed in instance (or retrieved by itself).