3

I'm writing a firefox plugin which adds a div to the top of the page with various things in it. I can change the content of the div when writing html code directly into the .innerHTML property.... but it looks very messy and it's hard to make changes to the code. I've read about iframes but it doesn't seem to work, nothing is shown but a vertical line. The object tag isn't working either. Can someone help me?

My code:

    var div = document.createElement('div');
    div.id='mainplugindiv';
    div.style.width = '100%';
    div.style.height = '150px';
    div.style.background = '#313192';
    div.style.color = 'white';

    div.innerHTML = "<iframe src=\"test.html\"></iframe>"; //not working
1
  • Maybe this helps... var iframe = document.createElement('iframe'); iframe.src = 'url'; Make sure to check in the console if the URL you require is found. (no 404 errors) Commented May 2, 2016 at 12:50

4 Answers 4

2

first we need to create a div and then create iframe object.then you can append iframe into created div and then append created div into Html body.

here it is,

<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script>
  $(document).ready(function() {
    var newDiv, iframe;
    newDiv = document.createElement("div");
    iframe = document.createElement("IFRAME");
    iframe.setAttribute("src", "http://en.wikipedia.org");;
    $(iframe).appendTo($(newDiv));
    $(newDiv).appendTo($('body'));
  });
</script>

<body>
  <p>testing
  </p>
</body>

</html>
Sign up to request clarification or add additional context in comments.

3 Comments

The question doesn't have a jquery tag, why would you suggest jquery for it?
Actually i just want to show working flow. how could replace inner html with outer file. if you want without jQuery. lets try this one. <html> <script> function apendIframe() { var newDiv,iframe; newDiv = document.createElement("div"); iframe = document.createElement("IFRAME"); iframe.setAttribute("src", "en.wikipedia.org"); newDiv.innerHTML = iframe.outerHTML; document.body.appendChild(newDiv); }; </script> <body onload="apendIframe()"> <p> testing </p> </body> </html>
my problem is, that I don't have access to the source html page. So it has to go in my .js file.
1

You could use Jquery for this.

The code below will load all the content from the htmlSheet.html into your div element.

 $.get("path/path/htmlSheet.html", function (htmlSheet) {
    $("#mainplugindiv").html(htmlSheet);
...
}

HTML after executing the code above:

<div id="mainplugindiv">
    //here will be the content of htmlSheet.html
</div>

3 Comments

The question doesn't have a jquery tag, why would you suggest jquery for it?
It is one possible solution.
Yes, but we should always respect the OP specification, maybe he doesn't need jquery .
1

In your actual code you are just creating a div, but not appending it to the page.

You can use document.body.appendChild(div) to append it to the body:

    var div = document.createElement('div');
    div.id='mainplugindiv';
    div.style.width = '100%';
    div.style.height = '150px';
    div.style.background = '#313192';
    div.style.color = 'white';

    div.innerHTML = "<iframe src=\"test.html\"></iframe>";
    document.body.appendChild(div);

EDIT:

The actual code works fine, I even tested it in Firefox and it gives the same result, if this is not your problem then maybe you need to be more specific with the result you are getting.

4 Comments

The questioner doesn't have the problem that the div is not appended to the body. "I can change the content of the div when writing html code directly into the .innerHTML property.... but it looks very messy and it's hard to make changes to the code."
it doesn't work... it shows the frame of the iframe, but no content, not for my local html or any other (like www.google.de)
@JaninePolster Maybe there's something wrong with your path?
not very likely, in that case there would appear a 404, like in the snippet above.. But mine is empty. I put the test html in every folder which could be the source but still...
0

You need to append element with html to show on web page.

Add HTML

    <div id="div1">

    </div>

And Update your Javascript

    var div = document.createElement('div');
        div.id='mainplugindiv';
        div.style.width = '100%';
        div.style.height = '150px';
        div.style.background = '#313192';
        div.style.color = 'white';

        div.innerHTML = "<iframe src=\"test.html\"><p>test</p></iframe>"; //not working

        var element = document.getElementById("div1");

        element.appendChild(div);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.