Import HTML file into another HTML - javascript

I'm trying to import HTML file into HTML.
I need something like this:
<div>
<iframe onload="iframe.document.head.appendChild(ifstyle)" name="log" src="Objects.html"></iframe>
<style>
iframe {
height: 300px;
position: absolute;
bottom: 5px;
width: 100%;
}
</style>
But <iframe> crushes all the code in the main HTML file and causes thousands of errors like:
Error: attribute points: Unexpected end of attribute. Expected number, "…5390592086273 0 ".
Also:
Blocked a frame with origin "null" from accessing a cross-origin frame at HTMLIFrameElement.onload
So I need to put HTML into another HTML and style it.

Use the element to link files from HTML, CSS, or javascript:
<html>
<head>...</head>
<body>
<link herf="blah.html">
</body>
</html>

Related

Unable to change the styling properties inside an iframe body tag

I have written this html script to display wikipedia inside iframe but i want to change the background color of it, below is the code snippet which i tried. but its not working.
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: blue !important;
/* Adding !important will give this rule more precedence over inline style */
}
</style>
</head>
<body>
<iframe style="background-color:#fc3 !important;" src="https://www.wikipedia.org/" width="100%" height="450px" >
</iframe>
</body>
</html>
You cannot modify the contents of an iframe before loading (using CSS), because it is displaying content from another page. After it loads, you may modify it with javascript.

window.onload won't call and won't create a `<base>` tag

I have this html page with js embedded in it
<html xmlns="http://www.w3.org/1999/xhtml">
<title>Versions: 1.0.715 vs. 1.0.715 [ route HISTORIC_TIME ]</title>
<head>
<script type="script">
function createBase() {
var str1 = window.location.href;
alert("check");
var base = document.createElement('base');
base.href = str1;
document.getElementsByTagName('head')[0].appendChild(base);
}
window.onload = createBase;
</script>
</head>
<body style="">
<div class="content-background" style="background-color: #4285f4; height: 398px; position: absolute; width: 100%; top: 60px; z-index: -1;"></div>
</body>
</html>
I try to add a <base> tag with the current url. However I refresh the page and the alert is not shown plus the <base> tag isn't created.
Why is that?
<script type="script">
script is not a registered mime type. It is not a programming language supported by any browser that I'm aware of. Since the browser don't know the language, it doesn't try to execute it.
The correct mime type for JavaScript is application/javascript (or text/javascript for legacy browsers).
As of HTML 5, the type attribute is optional and should be omitted entirely when writing JavaScript (since it is a waste of bytes and offers the opportunity to break code with typos).

How to isolate CSS rules inside a div?

I need to load an html page inside a div in the following pseudo page:
<html>
<head></head>
<style>
body {
background-color: yellow;
}
</style>
<body>
<div style="display:none">
<html>
<head></head>
<style>
body {
background-color: blue;
}
</style>
<body>
<div style="display:none">
...
</div>
</body>
</html>
</div>
</body>
</html>
What naturally happens in this code is that the background will turn blue, as it is being changed in the middle of the page. Is there a way to isolate this div? So it would act similarly to an iframe. The content inside the div is stored in a variable, so I think I cannot use a frame, as the html code is not stored in a file to use it as a source.
Thank you!
This is just wrong.
An HTML document can only have one html tag and one body tag, otherwise it will be an invalid document, browsers won't allow it.
If you load an iframe, instead, it will have his own #document and it's fine.
You can not load a Site into a Site without an Iframe due to security risks.
The only thing you can do, is to load the external Site with a serverside script like php, cut of the head with regexp and send the rest to your site into your div.

How to apply CSS to iframe using jQuery?

How do I access the iframe and override the elements style "td.rank" ?
This is what I've got so far:
<script>
var head = $("#iframe11").contents().find("head");
var css = '<style type="text/css">' +
'td.rank{background-color: #fc6b0a!important;}; ' +
'</style>';
$(head).append(css);
</script>
<iframe src="http://www.example.com" id="iframe11" style="margin-left: 174px; width: 401px; border: 0px solid black; height: 358px; opacity: 0.85; margin-top: 23px;"></iframe>
When I open the code using "inspect element" - I don't even see the CSS code part in the <head> tag of the iframe.
You if would like add css style inside iframe using jQuery then just follow below steps...
1. First create 'index.html' file and add below code in it
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.6.3.min.js"></script>
<script>
$(document).ready(function() {
$('#iframe').load(function() {
$("#iframe").contents().find("head").append("<style>.text_color{color:red;}#page{margin:0;}</style>");
});
});
</script>
</head>
<body>
<iframe src="iframe.html" id="iframe" name="iframe"></iframe>
</body>
</html>
2. Next create another file called 'iframe.html' and add below code to it
<!DOCTYPE html>
<html>
<body>
<div id="text"><span class="text_color">Content inside iframe goes here<span></div>
</body>
</html>
3. Next run 'index.html' file and now see 'Content inside iframe goes here' text color will changed to red color
Ensure that the frame document is on the same origin as the framing document so you don't have a security policy violation.
Move the script so it appears after the frame so that the element exists before you try to access it
Move the code into a function and use that as a load handler for the frame so that the DOM you want to manipulate exists before you try to manipulate it

jqLite: elm.css("width") is returning an empty string

This appears to be an issue with jqLite, which I came across while working with angular.js. To see the problem, open the console tab and click "run with js" in this jsbin. left.css("width") is returning an empty string when it shouldn't be.
HTML
<!doctype html>
<html ng-app>
<head>
<meta name="description" content="Angular Template" />
<script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<div class="container">
<div class="left"><span id="test">left</span></div>
<div class="right">right</div>
</div>
</body>
</html>
CSS
.container {display:table; width:100%;}
.left {border: 1px dashed purple; display:table-cell; overflow:hidden; width: 66%; height: 20px;}
.right {display:table-cell; background:green; width: 34%; height: 20px;}
JS
var innerSpan = document.getElementById("test");
var left = angular.element(innerSpan);
console.log(left.css("width"));
When I inspect the element using the chrome dev tools panel, the computed width is definitely not the empty string? What am I missing here?
You can't use .css('width') to get the width of an element. You use it to get the styled width. Since you didn't define the width in the element's style attribute, you get no value.
Try .prop('offsetWidth') instead.
Also, when using jsbin.com, your script is automatically included. Including script.js is just throwing a 404.

Categories