Create a new URL using `window.URL.createObjectURL` and specify the filename to open the PDF

Is there a way to customize the filename in createObjectURL of the blob? Currently, it generates a URL like this:
<URL>/bfefe410-8d9c-4883-86c5-d76c50a24a1d

const data = window.URL.createObjectURL(newBlob);
const pdfWindow = window.open();
pdfWindow.location.href = data;

I'm not looking to download the file (solution can be found on StackOverflow), instead, I want it to open in a new tab.

Answer №1

After researching answers from a Stack Overflow thread about setting tab titles in javascript window.open for displaying PDF files and another post on setting titles in popup windows, I experimented with the provided solutions, which proved effective across most web browsers.

const pdfData = window.URL.createObjectURL(newBlob);
const newPDFWindow = window.open();
newPDFWindow.location.href = pdfData;

windowTitle = "my-document.pdf";
// Set the title of the new window
newPDFWindow.document.title = windowTitle;

// Ensure that the window is fully loaded
newPDFWindow.onload = () => {
    // Add a delay to ensure the title change takes effect
    setTimeout(() => {
        newPDFWindow.document.title = windowTitle;
    }, 500)
}

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

Steps for retrieving a table of records with Jquery and sending it to the server through AJAX

Displayed below is a table I have: <table cellspacing="0" rules="all" border="1" id="ContentPlaceHolder1_GridView1" style="border-collapse:collapse;" class="table table-striped"> <tbody> <tr> <th scope="col"> ...

What could be the reason why my AJAX error is not displaying the exception from my Webmethod?

I am currently utilizing Google Chrome for my work tasks. After referring to , I attempted to throw an exception from a webmethod. However, no output is shown in the console.log. The only information I received is a generic error message from the network ...

Is it possible to simultaneously update two entities using a single endpoint?

In order to update data in two different entities with a @OneToOne relationship between UserEntity and DetailsEntity, I need to create a function in my service that interacts with the database. Here are the entity definitions: UserEntity @Entity() export ...

Acquire data from an array of objects using JavaScript

I've written some code where I'm attempting to display certain values in HTML. Specifically, I am looking to extract the values from the "current" object: ("dt":1643884851, "temp":8.11, "description":"few clouds", and "icon":"02d") In addition, ...

Bootstrap collapsible panel with unique off-center design

I am currently working on customizing a bootstrap collapsible panel that has an off-center indicator arrow. My goal is to center the indicator arrow with CSS. Here's the CSS code I have implemented: .panel-heading a:after { ...

Getting started with html2canvas: A beginner's guide

So here's a seemingly simple question... I'm diving into new territory and stumbled upon http://html2canvas.hertzen.com with a straightforward tutorial. After successfully running the command npm install -g html2canvas, I hit a roadblock. Where e ...

In jQuery selectors, providing two variables may not yield any results, yet inputting the same string manually produces the desired output

For instance: let a = "1234"; let b = "line1\\\\.5"; Now, this particular code line: "#" + a + b; Produces the following string "#1234line1\\.5" And when I use it in the select ...

Preventing variables from reinitializing in express.js

Within my app.js file, I invoke a search function that communicates with an LDAP server. Upon doing so, the server sends back a cookie which is essential for my subsequent queries. My intention was to save this cookie as an app.local variable in my app.j ...

Notify when a certain element's ID is visible on the screen using the jQuery appear method

Having trouble getting this plugin to cooperate: https://github.com/morr/jquery.appear I attempted to reference the plugin from a CDN using Ajax: http://cdnjs.cloudflare.com/ajax/libs/jquery.appear/0.3.3/jquery.appear.js Why isn't it working as expe ...

NodeJS Static method is not a valid function

I have encountered an issue with my NodeJS application where I am trying to access a static Method that performs a calculation Function. However, when attempting to access the Method, I received an error stating "isNotAFunction". Below is the static class ...

Possible revised text: "Exploring methods for verifying elements within a div using Selenium

I have a situation where I need to verify elements within a div by using the following xpaths. The xpath for each item is as follows: Item 1:- //*[@id='huc-last-upsell-rows']/div[1]/div[2]/div[1]/div/div/a/img Item 2:- //*[@id='huc-last-u ...

Utilizing JavaScript Plugin to Fix Image Source URLs in Incorrect Directories

I'm struggling and really in need of some assistance. The issue I'm facing is with a plugin that has a JavaScript file containing an array of URLs pointing to a "texture" directory within the plugin for images. However, I keep encountering 404 e ...

What is causing the role="status" attribute to malfunction?

I'm having an issue with the role="status" attribute in my code. When using a screen reader, the paragraph text doesn't get read once it's appended to the body. index.html: <!DOCTYPE html> <html> <head> <title> ...

Unable to invoke .then when simulating axios call within componentDidMount

I'm currently working on unit testing the componentDidMount method while simulating an Axios call. // src/App.tsx import axios_with_baseUrl from './axios-instance-with-baseUrl'; ... public componentDidMount() { axios_with_baseUrl.get(& ...

Utilize the power of jQuery for form validation by combining the errorPlacement and showErrors functions

I am currently attempting to implement validation using the Jquery .validate plugin. Unfortunately, I have encountered an issue where I am unable to utilize both the errorPlacement and showErrors methods simultaneously. If you'd like to see a demons ...

The code functions properly on a standalone device, however, it encounters issues when

I am facing an issue with my JavaScript code. It works perfectly fine when I run it on my local machine, but once I upload it to the server, it only functions properly after I refresh the page. Below is the code in question. Please do not hesitate to rea ...

Progressive enhancement with Three.js for different devices

Seeking recommendations on implementing progressive enhancement in Three js projects for varying devices. I have developed an app with post-processing effects that run smoothly on modern devices but lag on older or lower-end phones. I am looking for a wa ...

Upon initial launch of the application, JavaScript fails to load

I am currently working on developing a hybrid app that utilizes iOS as the native platform and Cordova-Phonegap for HTML support. Upon launching the application for the first time, we encounter an issue where the JavaScript fails to load. Although we are ...

What is the best way to retrieve multiple image file names using JavaScript?

I attempted to use the code snippet below to retrieve the names of all the images I selected, however when I used 'alert', I only received one name even though I selected multiple. My goal is to have all the names of the selected photos saved in ...

What is the best way to retrieve all classes that have a hyphen/dash in them from a DOM element and then store them in an array using JavaScript?

How can I retrieve all classes that contain a hyphen/dash applied to any DOM element and store them in an array using JavaScript? My current approach: const getClasses = []; const classesContain = []; document.querySelectorAll('*').forEach( ...