iFrame body font-family styling

Within the body tag, I have placed an iFrame:

<body onLoad="iFrameOn();">
    <iframe name="richTextField" id="richTextField" style="border:#000 1px solid; width:700px; height:300px;"></iframe>
</body>

The iFrameOn JavaScript function is structured like this:

function iFrameOn() {
    richTextField.document.designMode = 'On';
    richTextField.document.body.style.fontFamily = "Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif;
}

However, the font within the iFrame does not change as intended. The error console displays a message: "Helvetica is not defined(...)". How can I address this issue so that the specified font is applied to the iFrame?

Thank you for your assistance!

Answer №1

It is recommended to combine them into a single string,

richTextField.document.body.style.fontFamily = 'Open Sans, Helvetica Neue, Helvetica, Arial, sans-serif';

Answer №2

Modify the style of your iframe by incorporating a font-face declaration. Below is an example code snippet that successfully implemented this change:

var myframe = $('#iframeId');
var myframStyle = myframe[0].contentWindow.document.children[0].children[0].getElementsByTagName('style');
var myframeStyleContent = myframe[0].contentWindow.document.children[0].children[0].getElementsByTagName('style')[0].innerHTML;
var myfontFace = "@font-face {font-family: your-fontname; font-style: normal;font-weight: 400;src: url('your web font url') format('woff2');} body {font-family:your-fontname;}"; 
myframeStyleContent += myfontFace;
myframe[0].contentWindow.document.children[0].children[0].getElementsByTagName('style')[0].innerHTML = myframeStyleContent;

Answer №3

If you want to change the font family in your rich text field, you can use the code snippet below:

richTextField.contentDocument.body.style.fontFamily = '-apple-system, BlinkMacSystemFont, San Francisco, Segoe UI, Roboto, Helvetica Neue, sans-serif';

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

Retrieve precise information from JSON for use in React applications

Here is a sample JSON file : [{ "name": "bagette", "price": "0.200" }, { "name": "farine", "price": "1" }, { "name": "tomato", "price": "1.200" }, { "name": "chocola", "price": "4.000" }] I need assistance with extracting data from this J ...

In JavaScript, I am looking to duplicate an image and place it back at its original location after it has been moved from its initial position

I am working with 3 draggable images and my objective is to ensure that each image returns to its original position when moved. Ultimately, I want the user to be able to interact with multiple instances of images 1, 2, and 3 on the page, all of which are d ...

What is the best way to fix a div at the top, while anchoring the content at the bottom of the div?

Currently, I am using Pandoc to convert files to PDF and incorporating HTML/CSS for document styling, specifically for printing purposes rather than for screen display. My objective is to replicate the "header" of a Word document. To achieve this, I have ...

Running a Python Selenium script

I need some help executing this script using selenium. <div class="vbseo_liked"> <a href="http://www.jamiiforums.com/member.php?u=8355" rel="nofollow">Nyaralego</a> , <a href="http://www.jamiiforums.com/member.php?u=8870" rel="nofollo ...

Load different fonts dynamically based on environment configuration

My question is regarding loading multiple fonts from a font.css file in React. I need to make the font path dynamic based on the environment variables. Can anyone suggest a solution for achieving this? I've attempted to create a font.scss file and de ...

Glitches of white light during loading screens on your Prestashop website

There's a strange occurrence on the website where it flashes white DURING (not just at the start) every time a page loads. Initially, the page seems to load smoothly and almost completely, but then there is a sudden flash before all elements are disp ...

What is the best way to use JavaScript to conceal all div elements?

Can someone please provide a JavaScript solution to hide all divs on a web page without using jQuery? I am looking for a method that does not involve using the arrays that are associated with document.getElementByTag. If this is not possible, could you p ...

Is there a way to decrease the speed of a range slider?

Recently, I created a range slider using HTML, CSS, and JS to complement my Lua Nui. It's been working smoothly for the most part, but I've noticed an issue when sliding too quickly on the slider. As I slide it to the left, certain values fluctua ...

The Angular route functions flawlessly in the development environment, but encounters issues when deployed to

I have a project built with Angular 2, During development on localhost, everything runs smoothly. However, once I build a production version using (npm run build: prod) and navigate to the route on the server, I encounter a 404 error indicating that the r ...

Challenges encountered while creating a Number Tables Program (such as displaying 12 x 1 = 12) using Javascript

Currently, I am working on developing a program that can generate mathematical tables for any given number. For example: 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 To achieve this, the user should provide the following inputs: (1) The number they want ...

Encountering issues with the hyperlink tag '<a>' in an HTML document

I've encountered an issue with the code on my HTML page: <body> <div align="center"> <a href="../index.html"> <img border="0" src="banner1.jpg" width="800" height="120" alt="Ecommerce Knowledge Base"> &l ...

Image loading failure detected in ReactJS

In my current project using Reactjs (Nextjs framework), I encountered an issue where I am unable to display an image on a page without specifying the "height" and "width" attributes in the Image tag. I attempted the following code snippet but the image is ...

Retrieve values from a nested object using a variable in AngularJS

obj { val1 { nestedval {} } val2 { nestedval {} } } I'm having trouble accessing the nested value and assigning either val1 or val2 as a variable. I've been trying: var getVal = obj.varible.nestedval but it's not working f ...

Ways to elegantly re-establish a websocket connection?

I've been working on a single-page application that utilizes HTTP and Websockets. When the user submits a form, I start streaming a response back to the client. Here's a snippet of the client-side code: var html = `<!DOCTYPE html> <met ...

The command 'coconuts' is not acknowledged as an internal or external command, operable program or batch file

I'm currently facing some challenges with my first Cocos2d project. Despite searching for solutions, I haven't been able to resolve the issue. Whenever I try to create a new project, I receive an error message stating "cocos is not recognized as ...

Can someone assist me in creating a dynamic sprite using Javascript?

I've created a gif that I want to use for my sprite, but I'm having trouble getting it to play only when the character moves. Currently, I have a rectangle with choppy movements and I'd like to make them smoother. Despite watching numerous t ...

Guide to refining a JSON array using a pre-established list

I'm in need of assistance figuring out how to accomplish the following task: Below is the code snippet I am working with: public class Data { public string FirstName; public string LastName; public int Age; } var data = new Data { //this objec ...

Working with arrays and elements in JavaScript using regular expressions

The programming language is JavaScript. Imagine we have an array filled with positive and negative integers mixed in with other letters (e.g. 1a or -2d). The challenge is to figure out how to select all the strings that start with or contain positive inte ...

What is the best way to recover past messages from a channel?

I am working on a bot that is supposed to be able to retrieve all messages from a specific server and channel upon request. I attempted to use the channel.messages.cache.array() method, but it only returned an empty array []. How can I efficiently fetch ...

How can I expand the notification pop-up in R Shiny?

I'm trying to figure out how to expand the notifications in R Shiny because right now they are cutting off longer error messages. Any suggestions? library(shiny) ui <- fluidPage( actionButton("test", "Test") ) server <- f ...