Utilize Dojo to programmatically showcase an image

I'm facing an issue while trying to programmatically display an image using dojo. I have tried using both "dijit/Dialog" and "dojox/image/Lightbox". However, when using the Dialog method, instead of displaying the image, some random characters are shown in the dialog box. On the other hand, when using Lightbox, the image is always displayed on the second click.

imageDialog = new Dialog({
  title:  "my image",
  preload: false,
  href: "/ajax/fileStreamer"
});
imageDialog.show();

The code above only shows some characters, even if a relative path to the image is provided as input.

When using Lightbox with a relative path, the image displays properly. However, when using an image stream, the image is only displayed after the second click.

Answer №1

Lightbox automatically assumes that the file is an image and adds the <img /> tag internally to the href attribute. On the other hand, Dialog does not do this, so you need to manually include the <img /> tag in the content attribute instead. Depending on your desired outcome, there are several options available:

Using ContentPane:

        require(['dijit/layout/ContentPane'], function (ContentPane) {
            var theImage = new ContentPane({content: '<img  src="yourimageurl"/>'}, 'theImage');
            theImage.startup();
        });

Using Dialog (which extends ContentPane):

        require(['dijit/Dialog'], function (Dialog) {
            var theImage = new Dialog({title: 'the image', content: '<img  src="yourimageurl"/>'});
            theImage.startup();
            theImage.show();
        });

Utilizing Lightbox (requires Lightbox CSS, utilizes LightboxDialog which extends Dialog internally):

        require(['dojox/image/Lightbox'], function (Lightbox) {
            var theImage = new Lightbox({title: 'the image', href: 'yourimageurl'});
            theImage.startup();
            theImage.show();
        });

Using LightboxDialog (similar to Lightbox but exposes the instance of LightboxDialog):

        require(['dojox/image/Lightbox'], function (Lightbox) {
            var theDialog = new dojox.image.LightboxDialog({});
            theDialog.startup();
            theDialog.show({title: 'the image', href: 'yourimageurl'});
        });

Answer №2

Insert your image html code into the content attribute of your dialog box.

Here is a sample demonstration:

require([
  'dojo/domReady!'
], function() {
  require(["dijit/Dialog", "dijit/form/Button", "dojo/domReady!"], function(Dialog, Button) {
    var myDialog = new Dialog({
      title: "Creating Dialog Box Programmatically",
      style: "width: 300px"
    });

    var myButton = new Button({
      label: "Click to Open!",
      onClick: function() {
        myDialog.set("content", '<img class="lightbox-image" style="" src="https://placeimg.com/640/480/any">' + new Date() + "!");
        myDialog.show();
      }
    }, "progbutton");
  });
});
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/dojo/1.12.1/dijit/themes/claro/claro.css" />

<script>
  window.dojoConfig = {
    parseOnLoad: false,
    async: true
  };
</script>

<script src="//ajax.googleapis.com/ajax/libs/dojo/1.12.1/dojo/dojo.js">
</script>

<body class="claro">
  <p>Upon clicking the button, the dialog box will appear. Note that this time there is no existing content inside the dialog node:</p>
  <button id="progbutton" type="button">Show me!</button>
</body>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to retrieve the data from promises after they have been resolved?

I'm struggling to retrieve the values from getPeople(0,4). function getPeople(start, end) { const peopleArray = []; for (let i = start; i <= end; i++) { peopleArray.push( axios.get(`https://www.testsite.net/api/test/workers/ ...

A pair of variables combined within a set of single quotation marks

After exploring numerous examples, I am still struggling to include a variable inside quotes. This situation seems unique and difficult to resolve. Take a look at the code snippet below: Var x='http://www.xyzftp/myservice/service1.svc' Var y=&a ...

What is the method to determine the number of keys that have the same value in a JavaScript object?

My goal is to achieve functionality similar to this: var data = [ { tag:'A', others:'Abc' }, { tag:'B', others:'Bbc' }, { tag:'A', others ...

Display a list of dates within a specified range using the ng

Utilizing the ng-repeat directive to connect data with a filter for name search: <div ng-repeat='myoldrecs in myoldrec | filter:q as results '>......</div> $scope.myoldrec = [{name:ccc,date:13-02-2016},{name:ddd,date:14-02-2016}]; &l ...

Troubleshooting XhrIo issue with Google Closure on Internet Explorer

When I added the try block to the code below, it started alerting Error: Could not complete the operation due to error c00ce56e.. goog.require("goog.dom"); goog.require("goog.net.XhrIo"); goog.require("goog.structs.Map"); goog.require("goog.Uri.QueryDat ...

Access a specific JSON value using AngularJS

When using AngularJS to read a specific JSON value, I encountered the following issue: $http({method: 'GET', url: urlVersion}). success(function(data, status, headers, config) { console.log("success data " + status); $scope.ext = data.ve ...

Revamping status and backend systems

Seeking advice on the most effective method to execute a HTTP PUT request within my react application. A Post component is responsible for fetching data from https://jsonplaceholder.typicode.com/posts/1 and displaying it. There's another component na ...

Hide jquery scroll bar

I am currently working on a WordPress plugin with the Twenty Thirteen theme. My goal is to display a modal when a div is clicked, and at that time I want to hide the scrollbar on the body of the page. Despite trying the following code snippet, it doesn&ap ...

Function that contains a JavaScript reference and observation

I'm experiencing issues with the code below and I'm having trouble figuring out what's causing the problem. function some(){ for (var i=0;i<....;i++) { var oneObject; ...some logic where this object is set oneObject.watch(prop ...

Obtain custom parameter data from a string

Searching for custom parameter values within a string is necessary. For instance, given the following string: aaa[111] bbb[222] ccc[333] ddd[444] I am looking to extract the value 111 associated with the parameter aaa, and so on... The specific paramete ...

"Encountering difficulties while setting up an Angular project

I am currently working on setting up an Angular project from scratch. Here are the steps I have taken so far: First, I installed Node.js Then, I proceeded to install Angular CLI using the command: npm install -g @angular/cli@latest The versions of the ...

Experimenting with a Jest test on an express middleware

I'm currently faced with a challenge in testing my controller (express middleware) using Jest. To better illustrate the issue, I will share the code snippet below: import request from 'utils/request'; import logger from 'config/logger& ...

Loading an ASP.net page on the client side

Can someone tell me the event for pageload on clientside? function onPageLoad() { setSelectedIndexTo(1); } I attempted to do this, however no changes occur. Appreciate any help. Thanks! ...

What is the reason for webpack searching for jQuery even though it is not necessary for the module?

Before I proceed with my question, I want to clarify that I may not fully grasp the intricacies of this issue and I apologize if it does not meet the standards of Stackoverflow's Q&A. Currently, we are facing a challenge in our project while tryi ...

What is the reason for the emergence of this error message: "TypeError: mkdirp is not recognized as a function"?

While running the code, I encountered an error indicating that the file creation process was not working. I am seeking assistance to resolve this issue. The code is designed to fetch data from the Naver Trend API and Naver Advertising API, calculate resul ...

Merge a pair of observables to create a single combined output

Hey there, I'm currently diving into the world of RxJS and reactive programming. One challenge I'm facing is merging two observables. The first observable contains an array of objects called DefectImages[], while the second observable holds an ar ...

Open a webpage and execute JavaScript with just one click of a bookmark

I found a convenient bookmark that opens my Google Calendar page at http://www.google.com/calendar/renderOnline. Additionally, I have another bookmarklet that applies specific JavaScript to the page: javascript:document.getElementById('gadgetcell&apo ...

What should a Laravel controller return in response to an ajax request?

I encountered an issue in handling the response for an ajax request I initiated. Essentially, when a checkbox is changed(), it triggers an ajax call with a csrf-token that needs to be accepted via the POST method. The modifications are successfully made i ...

What is the best way to sort ISO DateTime objects that fall outside of a particular time frame?

I'm currently working on a project that involves a list of objects with properties for startTime and endTime, all in ISO format. Here's a snippet of the list: list = [ { startTime: '2022-06-26T10:00:00.000Z', endTime: '2022- ...

What is the reason for Cloud Firestore's realtime database fetching all items?

Currently, I am developing a project with vuejs and I am facing an issue where I need to fetch only the last created item from Firestore's realtime database. However, when I execute the following code snippet, it retrieves all items from the database ...