Jump to content

Recommended Posts

I'm working on a simple texteditor (yes, I know, there are many already) and the reason I don't want to use one already implemented is because they are too large for what I need, so, I created two buttons:

[*]one that will open a popup window (called preview) and write the content of the textarea in the popup window

[*]and another one (update preview) which should update the preview popup when clicked

My problem is that I cannot clear the content of the preview popup. I tried using the document.clear() function but it's not working as expected, that is the content is cleared alright but after that nothing works...

 

Do you guys have any clue about how can I clear the popup window's content so I can write the new updated content into it?

 

this is a part of my code [just to give you an idea] :

 



// create the preview window when the button is clicked
        preview_win = window.open('', 'preview', 'width=700,height=400,scrollbars=1,resizable=1');
        preview_win.document.write(self.editor.value);
        preview_win.focus();

// update the popup window's content when the content in the editor has been changed and the preview button has been clicked:
        if (preview_win && !preview_win.closed)
        {
            preview_win.document.clear(); // Clear content. The code below doesn't gets executed...
            preview_win.document.write(self.editor.value);
            preview_win.focus();
        }

Link to comment
https://forums.phpfreaks.com/topic/139601-solved-clear-popup-windows-content/
Share on other sites

I'm working on a simple texteditor (yes, I know, there are many already) and the reason I don't want to use one already implemented is because they are too large for what I need, so, I created two buttons:

[*]one that will open a popup window (called preview) and write the content of the textarea in the popup window

[*]and another one (update preview) which should update the preview popup when clicked

My problem is that I cannot clear the content of the preview popup. I tried using the document.clear() function but it's not working as expected, that is the content is cleared alright but after that nothing works...

 

Do you guys have any clue about how can I clear the popup window's content so I can write the new updated content into it?

 

this is a part of my code [just to give you an idea] :

 



// create the preview window when the button is clicked
        preview_win = window.open('', 'preview', 'width=700,height=400,scrollbars=1,resizable=1');
        preview_win.document.write(self.editor.value);
        preview_win.focus();

// update the popup window's content when the content in the editor has been changed and the preview button has been clicked:
        if (preview_win && !preview_win.closed)
        {
            preview_win.document.clear(); // Clear content. The code below doesn't gets executed...
            preview_win.document.write(self.editor.value);
            preview_win.focus();
        }

 

From what I've been able to dig up, the clear() method is deprecated and should not be used.  Instead, you have a couple of options:

 

You can create a new document by using document.open().

You can make the document contain only an empty string by using document.write('');

If your popup has a main content element, you can set its innerHTML to an empty string as well.

 

From what I understand, document.write() always adds text to the beginning of the document, rather than append to the end of the document.  If this is the case, then option 2 is probably your best bet.

Hi!

 

I tried different scenarios but none of them worked so far... It seems that the document.write method always appends the text... :(

 

maybe I'm missing something here, I don't know... I'll keep trying.

Hi!

 

I tried different scenarios but none of them worked so far... It seems that the document.write method always appends the text... :(

 

maybe I'm missing something here, I don't know... I'll keep trying.

 

Do you have a specific element that you're writing to in the popup window?

no, I don't but I've found my answer. You see, the first time I opened the popup window I was writing into it the whole content from the textarea and nothing else and after that, when I was trying to update the content, the new content was appended to the one existent one in the popup. So, I thought about what you said about innerHTML = '' and I changed the way I was initially writing the content in the popup, by that I mean that I inserted the basic html tags like a doctype head and body and inside the body I was then writing the content from the textarea. And that helped because when I'm now updating the popup all I have to do is get a reference to the popup's body tag and set to an empty string:

 

preview_win.document.getElementsByTagName('body')[0].innerHTML = '';

 

and then writing the new content.

 

thanks, Nightslyr ;)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.