Solved

iFrame questions (HTML, JavaScript, etc)

Posted on 2011-03-19
7
243 Views
Last Modified: 2012-06-21
Hello Experts,

Are there ways to manipulate iFrame tags so the focus is on a specifc area of a web page, and *not* the top-left corner by default?

0
Comment
Question by:RobWellsMc
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 10

Expert Comment

by:MiniDevo
ID: 35172853
There are multiple ways, including CSS, etc. Most iFrame examples should provide help;
http://friendlybit.com/css/frames-or-iframes-with-css/
0
 

Author Comment

by:RobWellsMc
ID: 35172882
Thanks MiniDevo - unfort the webpage you provide doesn't offer ways to manipulate an iframe, or alternative ways (CSS, jQuery, etc) to do exactly what an iFrame does which is to pull in web content from other pages.

If you know of websites that provide this info, then great!!!
0
 
LVL 10

Expert Comment

by:MiniDevo
ID: 35172912
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 10

Accepted Solution

by:
jtwcs earned 250 total points
ID: 35174217
0
 

Assisted Solution

by:NetGD
NetGD earned 250 total points
ID: 35176635
Hi,

Unfortunately it is not possible to set the scroll position of an iframe if you are loading a page that is on a different domain which is out of your control. This is a security feature of JavaScript that is illustrated in the following quote:

Now, javascript has a limitation - it can't access the document properties of pages which do not share the same domain as the parent page. Why? Because it will cause a security issue (known as cross domain security) and hence browsers do not allow that and if we try to access, it does not execute and gives a warning: Permission denied for <localhost:8080 [our page]> to get property HTMLDocument.body from <http://www.google.co.in [the external domain site]>.¹

As the previous quote shares with us, interacting with the iframe is not allowed when the domain is different from that of the parent page, or the page that the iframe resides on.

The good news is that I have come across a workaround that is fairly simple. The idea is to make the iframe as big as the webpage itself. Then wrap the iframe with a div that has an overflow set to “scroll.” Now this div will act as a layer mask with its own scroll bar, hiding the excess iframe behind it. Since the scrollbar of the div will be in your control, you can then scroll through the div to reveal parts of the elongated iframe.

Here is my own example below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Iframe Test</title>
<script type="text/javascript">
function myscroll()
{
	var obj = document.getElementById("wrapper");
	
	obj.scrollTop = 400;
}
</script>
<style type="text/css">
#wrapper {border:1px solid #000;display:block;width:1200px;height:500px;overflow:scroll;}
#loadedpage {width:1180px;height:3500px;}
</style>
</head>

<body onload="javascript:myscroll();">
<div id="wrapper">
<iframe id="loadedpage" src="http://www.amazon.com/" frameborder="0" scrolling="no"></iframe>
</div>
</body>
</html>

Open in new window

* Please note that I have removed the scrolling function of the iframe in my code to avoid double scrollbars. I have also removed the border to avoid an inner border, but also, since I am able to add it back onto the div through css if I wish.


I hope this makes sense,

NetGD



Sources:
¹ Quote was taken from Prakash Khanchandani ‘s post on http://www.liferay.com/community/forums/-/message_boards/message/7890856;jsessionid=C89925D99A8108601D4B0E6C0D1C9544.node-1
0
 

Expert Comment

by:NetGD
ID: 35176669
I’m sorry, I forgot to include the fact that you can also use "scrollLeft" in the JavaScript in the same fashion that I used scrollTop above, in order to scroll horizontally.
0
 

Author Closing Comment

by:RobWellsMc
ID: 35212466
Perfect, thanks!!!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to style different bullet points for unordered lists in CSS. Begin with a normal unordered list; the default bullet point is a solid circle: In the CSS, create a defined class of unordered list by typing "ul.S…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now