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 8339713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:57:20+00:00 2026-06-09T04:57:20+00:00

I’m attempting to insert a snippet into an iframe through jQuery with the following:

  • 0

I’m attempting to insert a snippet into an iframe through jQuery with the following:

var snippet = "<script>document.writeln('test')</script>";

jQuery('<iframe />').appendTo('body').append(snippet);

instead of writing “test” in the iframe, it overwrites the parent window.

How can I make it so that the parent window gets the iframe with “test” inserted in it?

  • 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-06-09T04:57:22+00:00Added an answer on June 9, 2026 at 4:57 am

    You should set the source of new windows and iframes to ‘about:blank’ if you want control over them. Then reference it by the iframe’s ID!

    You also want to use iframe.contentDocument || iframe.contentWindow.document
    And it might be a good idea to ‘open()’ the document before you ‘write()’ to it.

    Update: forgot this: If you open the window about:blank, it needs time to load..
    So you cannot write to it right away!!
    So either set timeout of about 50ms (usually) and then write to the new window/iframe.
    OR check if it is loaded (onload), then have it write the source (I prefer this).
    If you need to reuse the iframe, set it to about:blank first (again) and wait or check onload again before writing to the frame again. All this is due to xss security.
    I also strongly advise to use the traditional event-model (so no addEvent things, they don’t work as intended crossbrowser and lead to memoryleaks in IE).

    So: create iframe with src set to about:blank, add a onload function that checks a var containing your html-string: if it is empty, else: write it to the iframe and empty the string.
    To update the iframe: set content to the var containing the html-string, followed by setting the source of the iframe to about:blank.
    Understand the loop?

    This baby even works in IE6…

    Good luck!!

    UPDATE: you did not escape your snippet properly: should be: <script>document.writeln('test')\<\/script>
    See: jsfiddle

    UPDATE2: argl.. I normally never give up, but since I don’t care for jQuery, I’m not going through the manual for jQuery for something something so simple in something as difficult as jQuery (sorry). For modern (crappy) security reasons you need an about:blank. Period.

    See this updated fiddle for the ‘plain jane’ basics at work, then see how to do it in jquery and make a choice: onload or setTimeout. I’m working on my own crossbrowser html-editor and this subject took over a week to figure out.

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

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;

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.