Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    86
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Appending Javascript snippet via Javascript

    I want to insert this js snippet
    Code:
    function addText(smiley) {
        document.getElementById('message').value += " " + smiley + " ";
        document.getElementById('message').focus();
        return false;
    }
    to a loaded iframe with name&id chtifrm. I can access it & change embed something in its html via using something like:

    Code:
     $(parent.chtifrm.document.body).append('<div id=\"smly\" style=\"cursor:pointer;float:left;top:200px;display:none;position:absolute;\"><\/div>');
    ....
    Code:
    parent.chtifrm.document.getElementById('chatbox_option_disco').style.display == 'none'
    but how do I insert js in the head of loaded iframe?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,155
    Thanks
    203
    Thanked 2,548 Times in 2,526 Posts
    If the iframe does not originate in your domain, you can't. Security. Simply stated, the Same Origin Policy states that JavaScript code running on a web page may not interact with any resource not originating from the same web site.


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    86
    Thanks
    20
    Thanked 0 Times in 0 Posts
    yes sir I know that ...the iframe is of the same domain...but that wasn't the question. The question was that how do we insert a javascript snippet in the head section of any webpage...in my case the webpage was loaded in an iframe #chtifrm.

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Like this
    Code:
    var iframeDoc = document.getElementById('chtifrm').contentWindow.document;
    var iframeHead = iframeDoc.getElementsByTagName('head')[0];
    var newScript = iframeDoc.createElement('script');
    newScript.innerHTML = "var whatever_your_code_is = 5;";
    newScript.type = "text/javascript";
    iframeHead.appendChild(newScript);

  • Users who have thanked devnull69 for this post:

    defencedog (01-18-2012)

  • #5
    New Coder
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    86
    Thanks
    20
    Thanked 0 Times in 0 Posts
    sir like this:
    only a part of it I need clarification
    Code:
    newScript.innerHTML = "function addText(smiley) {\n    document.getElementById(\'message\').value += \" \" + smiley + \" \";\n    document.getElementById(\'message\').focus();\n    return false;\n}";

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,155
    Thanks
    203
    Thanked 2,548 Times in 2,526 Posts
    If the iframe is in the same domain, why do you not simply add the script to the page directly?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    New Coder
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    86
    Thanks
    20
    Thanked 0 Times in 0 Posts
    ahh thnx devnull; your code works besides I have learned something new...thnx again


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •