I want to display a source web page with some information about it above the page in HTML. I initially though of doing this with frames, but as far as I can tell a frame can only display an HTML src, not raw html. To b e clear, if the frame tag was allowed to enclose raw html, I would want to do something like
<frameset rows="200,2*">
<frame name="info>
<h3>Stack Overflow</h3>
<p>A site for programming questions</p>
</frame>
<frame name="site" src="http://stackoverflow.com">
</frameset>
What is the right way to do this?
You can use something like this:
In your HTML page, use:
<iframe src="get.php?url=http://google.com"></iframe>
And then, you create a php (or your server language) page:
<?php
header('Content-Type: text/plain; charset=utf-8');
echo file_get_contents($_GET['url']);
?>
This way you can load any webpage, and you even can change it with javascript without reloading your html page. But be aware that everybody can get any webpage using your get.php page.
There are several ways to do this:
You can put the HTML-source code either inside a <pre> block, which leaves it raw, but it might break if your HTML-source itself contains a <pre> block.
You escape the HTML code up front, e.g. using one of the online tools like http://www.htmlescape.net/htmlescape_tool.html
You can load the HTML code with javascript, for example using jQuery and put it inside a <div> using the jQuery text function
Because this is not 1994 the best way is to not use frames. Joking aside sometimes frames can still be useful, but should be avoided if possible. The reasons for this are
You create a brand new window object. Unless you need a new window object you are wasting memory.
Some search engines used to not follow the URLs in frames. This might not be the case anymore for destinations on the same domain, but can still alter how your pages will be ranked and unless you know what you are doing you shouldn't use frames for sites that you want indexed.
If you must to use frames you will have to use a separate html file for each frame, or you could use an Iframe. But an iframe still comes with the above caveats.
You could also dynamically load a div or a td with JavaScript (jQuery, prototype, mootools, etc.. can help) but from an SEO perspective that's even worse than frames, and should be reserved for apps that require login, even then, they will stop back from working.
The closest thing to a frameset from a positioning point of view is the table. You can leave one column width unspecified and set the table width to 100% and that column will take up the maximum space available like the framesets "*" in the rows attribute. However the height of a table can not be set to 100% of the window without javascript so you can't do the same for height and rows. Here absolute positioning will help more than a table. For example
<div style="position: absolute; top: 0px; bottom: 0px; width: 100%">
<div style="height: 200px; width: 100%; background: red;">Fixed Row</div>
<div style="position: absolute; top: 200px; bottom: 0px; width: 100%; background: blue">Fluid Row</div>
</div>
Will behave like frame rows if they are the root element. To work inside another element that element must have width, height and positioning:relative.
Setting a fixed height and overflow:scroll in the style of any element, like a td or a div, in the earlier examples will give it scroll bars like a frame. Then you can have all the data on one page or include an iframe with 100% width and height for content on a different url, though as I mentioned the same caveats apply as with the frameset.
Related
Is it possible to have a non-scrolling iFrame inside a scrolling div, and have anchor links inside the iFrame work correctly? The anchor links inside the iFrame should scroll to the spot inside the iFrame, I don't need/want them to point to elements on the parent page.
Here is my jsFiddle with a simple example:
http://jsfiddle.net/shopguy/WjmHG/
and the code for it:
<div style="width: 100%; height: 300px; overflow: auto;">
<iframe style="width: 100%; height: 2000px;" src="http://www.hypergurl.com/anchors.html" scrolling="no"></iframe>
</div>
I have no association with that hypergurl.com link used in my example, it was just the first example I could find of a page with an anchor by id/name syntax link in it.
If you load the JSFiddle and click the "Jump to Bottom" link inside the iFrame, it does nothing (testing with FireFox 19.0.2). When testing with various pages it never works in FireFox, in Chrome it sometimes works the first time it is clicked, but then if you scroll up and click again it doesn't work. In IE8 it works (scrolls) most of the time.
Scrolling works correctly all of the time if I let the iFrame itself have the scrollbars (remove scrolling="no"). This isn't a practical solution for me though, as I have content outside of the frame that I want to scroll with it. In my real code I dynamically set the height of the iFrame to fill its content, this way it appears to be more like content on my page.
Additional info as to why I need to do this:
I'm creating a web-based email client and so far there seems to be the least amount of issues if I display the HTML body of emails inside an iframe, vs trying to display inside a table cell or div inside my page. I'd like for these type of links to work. I do have some control over the content, it comes from my server and I can modify it some (but don't want to hack it too much). For example, I already modify all links to open in a new window (but not links that start with #, so that isn't my issue).
I know GMail doesn't use iFrames, but my XFINITY (by Comcast cable) web-based email client does, and they managed to get these to work (but so far haven't figured out what all they are doing).
Check out this post: Jump Link Inside an iFrame
If your iframe has a different domain then you will be unable to use a javascript solution to solve this, but if it is then you can add the target="_parent" attribute to all the anchors within the iframe.
var iframe = document.getElementById('iframeId');
var doc = (iframe.contentDocument)? iframe.contentDocument: iframe.contentWindow.document;
var anchors = doc.getElementsByTagName('a');
for (var i = 0; i < anchors.length; i++)
anchors[i].target = '_parent';
I've recently added code to this library to sort out all the issue with Anchor Links inside an iFrame.
https://github.com/davidjbradshaw/iframe-resizer
It intercepts all requests for in page navigation and scrolls the parent page to the correct position. If it doesn't find the anchor in the iFrame, it bubbles it up to the parent page and looks for it there.
So,
I have a loop that outputs a bunch of stuff from an SQL database. It also outputs a form with some of those outputs.
that entire loop sequence is displayed through an iframe embedded into one column on my site.
I dont know that this is a proper approach but it seemed to work well.
However, when users click "submit" in the form. The link functions correctly, BUT the page it brings the users to is still displayed within the iframe.
I instead want them to be taken to the new page in full.
Any advice?
I should clarify: the purpose of the iframe was to create a scrollable window/box so that if the outpuot was more lines then my template it wouldnt be an issue. Maybe there is another solution thats simpler.
You don't need an iframe to make a scrollable box. Just specify size for an element and use the overflow style to make it scrollable.
Example:
.formContainer { width: 300px; height: 500px; overflow: auto; }
In the form specify a target attribute so it will load in the full window
<form target="_top">
I'm not really sure I'd use an IFrame for this simply because it acts on it's own. I'd suggest looking into using AJAX. W3schools has a pretty good article on this if you're not sure where to get started.
Here's the way I've set-up my site. I have a page called "news.html". The content of this page is just an iframe with a fixed size. The iframe links to "innernews.html", which is the actual content I'm trying to display. I have it set-up this way to keep every page consistently sized. The iframe prevents the height of the page from expanding due to extra content, and is scrollable.
How would I create a link targeting a specific element/header within my "innernews.html" page? If there isn't a way to achieve this, I'll remove the iframe and just plug content straight into "news.html". But still I wouldn't know how to create a link that targets a specific element/header...
You can link to an element (on another page or on the same page) only if the element has the id attribute or it is an a element with the name attribute. In both cases, put the fragment identifier #foo at the end of the URL in the link, where foo is the value of the attribute.
If the page being linked to does not contain such an attribute, and if it is outside your control, you are out of luck
Basically, you can simply create a link to specific header of a page:
<a name="your_header_name"></a>
<h1>Header Text</h1>
...
Link to the header
I strongly recommend you to remove iframes from the page if there is no reason to keep them. Iframes can complicate your life when you're trying to do something not trivial.
Have you considered using a container such as:
#newsContainer {
overflow: scroll;
height: /*whatever*/
}
If I put a simple iframe within a DIV any DIV's below it do not show up, the page stops there. If I just type some text with no iframe, it works fine—so it is the adding of the iframe that causes it.
The file loaded by the iframe is dummied right down and just displays the word TEST.
Before I start posting a lot of code and stuff, is this generally an issue—can an iframe be used within a DIV statement?
Thanks
Sounds like you're making a pretty common coding error: you can't short-tag an iframe. For instance:
<iframe src=" ... " />
is invalid. You must provide a full closing element:
<iframe src=" ... "></iframe>
It's silly, but it can cause your page to completely gum up. Also, check to make sure your quotes/apostrophes/carets are all proper, as those can cause similar problems.
Hope this helps!
There's no reason you can't wrap an Iframe in a DIV, but Iframes are notoriously difficult in layout and obviously we don't know what other styling rules might be affecting your box model.
You might try giving the containing DIV an 'overflow:scroll' or even just 'overflow:auto' in your CSS. This can help fix strange box model issues.
For an iPhone ebook application I need to break arbitrarily long HTML documents up into pages which fit exactly on one screen. If I simply use UIWebView for this, the bottom-most lines tend to get displayed only partly: the rest disappears off the edge of the view.
So I assume I would need to know how many complete lines (or characters) would be displayed by the UIWebView, given the source HTML, and then feed it exactly the right amount of data. This probably involves lots of calculation, and the user also needs to be able to change fonts and sizes.
I have no idea if this is even possible, although apps like Stanza take HTML (epub) files and paginate them nicely. It's a long time since I looked at JavaScript, would that be an option worth looking at?
Any suggestions very much appreciated!
update
So I've hit upon a possible solution, using JavaScript to annotate the DOM-tree with sizes and positions of each element. It should then be possible to restructure the tree (using built-in XSLT or JavaScript), cutting it up in pages which fit exactly on the screen.
Remaining problem here is that this always breaks the page on paragraph-boundaries, since there is no access to the text at a lower level than the P-element. Perhaps this can be remedied by parsing the text into words, encapsulating each word in a SPAN-tag, repeating the measurement procedure above, and then only displaying the SPAN elements that fit onto the screen, inserting the remaining ones at the front of the next page.
All this sounds rather complicated. Am I talking any sense? Is there a simpler way?
You should look at the PagedMedia CSS module: http://www.w3.org/TR/css3-page/
CSS3 also support multicolumn layouts (google for "css3-multicol". I don't have enough Karma to include a second link here :-)
About your update: how about doing the layout of one single page, then use a DIV with overflow:hidden for the text part. Next thing would be to overlay a transparent item on top of that, that would programmatically scroll the inner content of the DIV PAGE_HEIGHT pixels up or down according to some navigation controls (or gestures).
The other option is to have a parent <div> with multiple css3 columns: link1, link2.
This works on Android:
<style type='text/css'>
div {
width: 1024px; // calculated
-webkit-column-gap: 0px;
-webkit-column-width: 320px; // calculated
}
p {
text-align: justify;
padding:10px;
}
</style>
The CSS multicol suggestions are very interesting! However, and I hope it's ok to respond with another question: how would you go from splitting one or more long <p> elements into columns to having one particular of these columns being rendered in a WebView? The DOM hasn't changed, so you can't pick out an element and render it. What am I missing?