Solved

iFrame questions (HTML, JavaScript, etc)

Posted on 2011-03-19
7
252 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing CSS, there are a few simple rules that will make your life easier.    1. Using ‘* {box-sizing:border-box;}’. Using this will wrap all your elements in a nice little compact box-model that will give you the width you want, like so... …
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this Micro Tutorial viewers will learn how to create a CSS image sprite (In a later tutorial, viewers will learn how to use CSS and HTML to create a navigation menu using this sprite) Open a new Photoshop document with a width of (Icon width)x(N…

785 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