Solved

iFrame questions (HTML, JavaScript, etc)

Posted on 2011-03-19
7
276 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

617 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