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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:20:37+00:00 2026-05-23T04:20:37+00:00

I am developing a system that allows userwritten javascript in widgets. To keep things

  • 0

I am developing a system that allows userwritten javascript in widgets. To keep things secure, I plan to sandbox these widgets in iframes. Of course, for the sandboxing to be effective the iframe must have a different domain than the parent document.

I would really love to be able to dynamically generate the iframe with code similar to this:

template = '<html><body><script>/* user code */</script></body></html>'
src      = 'javascript: document.write("' + template + '")'
widget   = $('<iframe>').attr('src', src)

$('#container').append(widget)

…and then have the resulting iframe be treated as cross-domain from the parent window. Is this possible, and if so, how would it be done?

  • 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-23T04:20:37+00:00Added an answer on May 23, 2026 at 4:20 am

    Ok, I think I get what you need but it’s a bit tricky.
    You want to create an <iframe> and populate it with the user Javascript client-side but still have the client sandboxed?

    This is fairly non-standard. Usually the contents of the <iframe> are generated server side. But here it goes.

    First some background: documents can not access the content of any document that is not from the same domain (including sub-domain) and port. But they can change their own security domain using the document.domain property. So what you need to do is lighten up the security then tighten it back up again for the user script to run.

    So you can’t do it the way you specified because if you create an <iframe> with a Javascript src the document.domain will match the parent frame. This means that the widget will have full access to everything.

    So here’s how you can do it:

    1. Set up two sub-domains of your main domain. Let’s call them home.example.com and widgets.example.com.
    2. Create a basic HTML file on widgets.example.com and make sure it calls this javascript: document.domain = "example.com";
    3. Now create your page that will contain all these widgets. Set it’s document.domain to the same value.
    4. Create all your iframes loading your basic HTML page from widgets.example.com into it.
    5. Set a variable inside the frame that contains the user template. Ex: myFrame.contentWindow.foo = "template";
    6. Switch the document.domain on the main window back to home.example.com so that the <iframe>s will no longer have access to the parent frame
    7. Trigger the template substitution in the frame

    That last part is the tricky part. You can’t just embed the code because if it runs automatically it will run before you can change the domain of the home document back, which will be a security issue. So instead you need to set it to a temporary variable inside the frame then somehow trigger the frame to replace its own contents with that template but only after everything is locked down. The easiest and most compatible way would be to trigger it on resize and then just change the width or height of the frame.

    Now, alternatively, if the widget was populated server-side:

    1. Host widget on widgets.example.com
    2. Host page containing widget on home.example.com
    3. Done

    But I assume that you have reasons for doing it all client-side.

    The logical next topics: communicating between the frames and auto-sizing. But those are for another day.

    Did I answer your question? I hope so because this was a lot of typing and I won’t mind the reputation points if you vote up and accept my answer! 😉

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

Sidebar

Related Questions

I'm developing a system that allows developers to upload custom groovy scripts and freemarker
I'm developing a set of applications that work together to create a system for
For a contact management system web app that allows tennants to upload lists of
I am developing an application that allows for a user to manage some individual
I'm developing an extension to MATLAB's PsychToolbox that allows for better control of the
I'm developing a Windows Form application that will typically minimize to the system tray.
I'm developing a system that needs to accept sql queries dynamically. I'm looking for
I'm developing a system with codeigniter, php and jquery these days, still in the
I have a web-based documentation searching/viewing system that I'm developing for a client. Part
I'm developing an email marketing system which allows users to send emails to multiple

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.