Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7595955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:48:06+00:00 2026-05-30T21:48:06+00:00

I’ve been trying to add HTML content to a web view, but can’t seem

  • 0

I’ve been trying to add HTML content to a web view, but can’t seem to get any results. The Apple documentation is very sparse and of the few examples I’ve found online, I can’t seem to get any of them working. As far as I can tell, this is how it’s supposed to work, but doesn’t.

To start, I have a very simple HTML document that I load into the webview. I’m trying to add some HTML content to the #container div

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <title>HTML TEST</title>

        <style type="text/css">       
            .test {
                display: block;
                border: #000 1px solid;
                width: 100px;
                height: 100px;
                background-color: #0F0;

            }            
         </style>
    </head>
    <body>   
        <p>hello world</p>
        <div id="container"></div>
    </body>
</html>

The WebView is inside a custom view. The header is very straight forward.

// NodeTest.h
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>

@interface NodeTest : NSView
@property (nonatomic, retain) IBOutlet WebView *webview;
@end

So, in awakefromnib, I load the basic html document, attempt to get the container div and insert an new HTML element into it, with the class name “test”. Which, according to the HTML file should have a black border, green fill and should be 100x100px.

#import "NodeTest.h"

@implementation NodeTest

@synthesize webview;

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }

    return self;
}

- (void) awakeFromNib {
    // Load the HTML document into the webview
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"TestDocument" ofType:@"html"]];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    [[webview mainFrame] loadRequest:req];

    [webview setEditable:YES];

    DOMDocument *domDoc = [[webview mainFrame] DOMDocument];

    // Get the container div. I've tried this with and without '#'
    DOMHTMLElement *container = (DOMHTMLElement *)[domDoc getElementById:@"container"];

    // Create a new HTML element
    DOMHTMLElement *myDiv = (DOMHTMLElement *)[domDoc createElement:@"div"];
    [myDiv setInnerHTML:@"<p>Boo!</p>"];
    [myDiv setClassName:@"test"];

    // Add the element to the container div
    [container appendChild:myDiv];

    [webview setNeedsDisplay:YES];

}

- (void)drawRect:(NSRect)dirtyRect
{
    // Drawing code here.
}

@end

Loading the HTML content into the WebView is fine. It’s all there. But I can’t for the life of me figure out how to add new HTML elements. If anyone has any pointers, I’d love to hear them. This is driving me mad.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:48:07+00:00Added an answer on May 30, 2026 at 9:48 pm

    WebKit loads requests asynchronously, so you need to spin the run loop after calling -[WebFrame loadRequest:] until the load is complete, and only then try to access the DOM. The common way to spin the run loop is just to return from -awakeFromNib, but you can also spin a nested run loop using the NSRunLoop APIs if needed. The standard way to find out when the load is complete is to give the WebView a WebFrameLoadDelegate and wait until its -webView:didFinishLoadForFrame: method is called. (If you are loading a resource that contains subframes you’ll want to wait until the main frame is passed to that method.)

    Once the load is complete the code you have listed that accesses -DOMDocument, etc., should work just fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.