The Spanner logo
    • Home
    • Blog
      • Blog home
      • RSS
    • Login
    • Home
    • Blog
      • Blog home
      • RSS
    • Login
    The Spanner logo

    The Spanner
    Web security blog

    Made by Gareth Heyes
    Follow me on Twitter: @garethheyes

    Javascript for hackers!

    Hackvertor logo
    Shazzer logo
    My Github account
    Recent posts
    Introducing Feedworm: A Privacy-First RSS Reader That Lives in DevToolsSpeedy RSVP extensionAutoVaderHackvertor history and tag finderShadow Repeater v1.2.3 releaseBurp Hackvertor v2.1.24 releaseHacking roomsXSSing TypeErrors in SafarivalueOf: Another way to get thisMaking the Unexploitable Exploitable with X-Mixed-Replace on FirefoxThe curious case of the evt parameterCSS-Only Tic Tac Toe ChallengeRewriting relative urls with the base tag in SafariBypassing DOMPurify with mXSSNew IE mutation vectorHow I smashed MentalJSMentalJS DOM bypassAnother XSS auditor bypassXSS Auditor bypassBypassing the IE XSS filterUnbreakable filterMentalJS bypassesmXSSJava SerializationBypassing the XSS filter using function reassignmentRPOSandboxed jQueryX-Domain scroll detection on IE using focusEpic fail IEnew operatorDecoding complex non-alphanumeric JavaScriptHacking FirefoxDOM ClobberingBypassing XSS AuditorThe evolution of codeNon-Alpha PHP in 6-7 charsetTweetable PHP-Non AlphaMentalJS for PHPOpera x domain with video tutorialSandboxing and parsing jQuery in 100ms

    Fresh prototypes on all browsers

    By Gareth Heyes (@hackvertor)

    Published 16 years 9 months ago • Last updated March 22, 2025 • ⏱️ 2 min read

    ← Back to articles

    So there's a well known technique for getting Object prototypes that are not from the current window which results in a fresh prototype. You use iframes to copy the required prototype from the iframe.contentWindow BUT...It doesn't work in all browsers and it's pretty silly having to copy each object manually, why not just use the window? Well you can :D

    So after a lot of code testing/rewriting here is how to do it:-

    <pre lang="javascript"> var iframe = document.createElement('iframe'); iframe.style.width = '1px'; iframe.style.height = '1px'; iframe.frameborder = "0"; iframe.style.position = 'absolute'; iframe.style.left = '-100px'; iframe.style.top = '-100px'; document.body.appendChild(iframe); var code = "(function(objConstructor){ return window.NameOfInstance= objConstructor();})(" + objConstructor+ ")"; if (window.opera) { iframe.contentWindow.Function(code)(); } else { iframe.contentWindow.document.write('<\script type="text/javascript">' + code + '<\/script>'); iframe.contentWindow.document.close(); } var obj = iframe.contentWindow.NameOfInstance; if(!obj) { iframe.contentWindow.Function(code)(); obj = iframe.contentWindow.NameOfInstance; } </pre>

    So here obj contains our Object instance within the context of the iframe window, that means any references to window inside your object only affect the iframe context. The reason for the if statements and different code is because Firefox, Safari, Opera and IE all act differently. Opera doesn't pass the object straight away unless the Function constructor is used, Safari supports the Function constructor method and the document.write method but doesn't return the object correctly when using document.write until it's loaded.

    The important part about this code is that you don't need to use the onload event of the iframe as the object is returned instantly :)

    ← Back to articles