Link to home
Start Free TrialLog in
Avatar of CalDev
CalDev

asked on

Load multiple pages and in iFrame via links and URL variable

I'm using the dynamic drive iFrame code SSI version 2 to load multiple links into an iframe: http://www.dynamicdrive.com/dynamicindex17/iframessi2.htm

The SSI iframe code enables the ability to load pages into the iFrame on the page by clicking a coded link. You should know that the code has also been modified to load a web address via a URL variable, if one exists,  into the iFrame when the page is loaded. For example if the address for my page containing the SSI iframe script was mypage.html and the page was addressed like this: mypage.html?URL=http://www.google.com the value in the variable "URL" (in this case "http://www.google.com") would be passed to the iFrame. This was all working until I changed the code so that the links, to pass the web pages to the iframe, were inside of drop down menus. I should say it worked in both FireFox and IE but now only works in IE.

The reason that it doesn't work in FF is that FF can not find the "loadintoIframe" function. The error I get is: "loadintoIframe is not defined".

Can anyone figure out how to adjust the code so that FF can access the "loadintoIframe" function?

Here is a simplified example of the code I'm running on the page:
<!----------- Multi-iFrame resize and URL variable passing code Start ---------->

<!-- This is image is used to pass the onload information since I don't have access to the body 
tag in the CMS this page is running in -->
<img src="http://mydomain.com/img/spacer.gif" width="1" height="1" onLoad="setURL('myframe');">

<script type="text/javascript">

/***********************************************
* IFrame SSI script II- © Dynamic Drive DHTML code library (http://www.dynamicdrive.com)
* Visit DynamicDrive.com for hundreds of original DHTML scripts
* This notice must stay intact for legal use
***********************************************/

//Input the IDs of the IFRAMES you wish to dynamically resize to match its content height:
//Separate each ID with a comma. Examples: ["myframe1", "myframe2"] or ["myframe"] or [] for none:
var iframeids=["myframe"]

//Should script hide iframe from browsers that don't support this script (non IE5+/NS6+ browsers. Recommended):
var iframehide="yes"

var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
var FFextraHeight=parseFloat(getFFVersion)>=0.1? 16 : 0 //extra height in px to add to iframe in FireFox 1.0+ browsers

function resizeCaller() {
var dyniframe=new Array()
for (i=0; i<iframeids.length; i++){
if (document.getElementById)
resizeIframe(iframeids[i])
//reveal iframe for lower end browsers? (see var above):
if ((document.all || document.getElementById) && iframehide=="no"){
var tempobj=document.all? document.all[iframeids[i]] : document.getElementById(iframeids[i])
tempobj.style.display="block"
}
}
}

function resizeIframe(frameid){
var currentfr=document.getElementById(frameid)
if (currentfr && !window.opera){
currentfr.style.display="block"
if (currentfr.contentDocument && currentfr.contentDocument.body.offsetHeight) //ns6 syntax
currentfr.height = currentfr.contentDocument.body.offsetHeight+FFextraHeight; 
else if (currentfr.Document && currentfr.Document.body.scrollHeight) //ie5+ syntax
currentfr.height = currentfr.Document.body.scrollHeight;
if (currentfr.addEventListener)
currentfr.addEventListener("load", readjustIframe, false)
else if (currentfr.attachEvent){
currentfr.detachEvent("onload", readjustIframe) // Bug fix line
currentfr.attachEvent("onload", readjustIframe)
}
}
}

function readjustIframe(loadevt) {
var crossevt=(window.event)? event : loadevt
var iframeroot=(crossevt.currentTarget)? crossevt.currentTarget : crossevt.srcElement
if (iframeroot)
resizeIframe(iframeroot.id);
}

function loadintoIframe(iframeid, url){
if (document.getElementById)
document.getElementById(iframeid).src=url
if (window.attachEvent) window.attachEvent("onload", loadintoIframe);
}


if (window.addEventListener)
window.addEventListener("load", resizeCaller, false)
else if (window.attachEvent)
window.attachEvent("onload", resizeCaller)
else
window.onload=resizeCaller

</script>

<script type="text/javascript">
function getUrlVars() 
{ 
var vars = [], hash; 
var hashes = self.location.href.slice(self.location.href.indexOf('?') + 1).split('&'); 
for(var i = 0; i < hashes.length; i++) 
{ 
hash = hashes[i].split('='); 
vars.push(hash[0]); 
vars[hash[0]] = hash[1]; 
} 
return vars; 
} 
 
var urlvars = getUrlVars(); 
 
function setURL(objid){ 
        var obj = window.document.getElementById(objid); 
        if(urlvars['URL']){ 
                obj.style.display = "block";  //show iframe 
                obj.src = urlvars['URL'];   //redirect iframe content to URL variable 
        } 
 
} 

document.observe("dom:loaded", function(){ 
 
setURL('myframe'); 
 
});

</script>

<!----------- Multi-iFrame resize and URL variable passing code End ---------->


<!--------------------  Dropdown menu code begin ----------------------------->

<!-- NOTE: for the purpose of this question I have linked to the files on the menu authors site -->

<link rel="stylesheet" type="text/css" href="http://www.stunicholls.com/menu/pro_dropdown_3/pro_dropdown_3.css" />

<script src="http://www.stunicholls.com/menu/pro_dropdown_3/stuHover.js" type="text/javascript"></script>

<ul id="nav" style="z-index:200;">
	<li class="top"><a href="#" id="services" class="top_link"><span class="down">Search Engines</span></a>
		<ul class="sub">
			<li><a href="#" class="fly">Major</a>
                  <ul>
                    <li><b>Top 3</b></li>
                    <li><a href="javascript:loadintoIframe('myframe', 'http://www.google.com')">Google</a></li>
                    <li><a href="javascript:loadintoIframe('myframe', 'http://www.yahoo.com')">Yahoo</a></li>
                    <li><a href="javascript:loadintoIframe('myframe', 'http://www.yahoo.com')">Bing</a></li>
                    <li><b>other</b></li>
                    <li><a href="javascript:loadintoIframe('myframe', 'http://www.ask.com')">Ask</a></li>
                </ul>
            </li>
        </ul>    
	</li>
</ul>

<!--------------------  Dropdown menu code end ----------------------------->


<!--------------------  iFrame code begin ----------------------------->
<div style="height:2000px;">
<iframe id="myframe" src="" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" vspace="0" hspace="0" style="overflow:visible; height:100%; width:100%; display:none; border:0px; position:relative; left:-20px;"></iframe>
</div>
<!--------------------  iFrame code end ----------------------------->

Open in new window

Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

It is already poor practise to use javascript: protocol in your href instead of the safer onclick event handler. In this case it is surely made worse by whatever the un-posted menu script is doing to your links. Please post a link to your page so we can debug the full menu
I also do not understand this part of the code which would be called each time you click

function loadintoIframe(iframeid, url){
if (document.getElementById)
document.getElementById(iframeid).src=url
if (window.attachEvent) window.attachEvent("onload", loadintoIframe);
}

I would code

function loadintoIframe(iframeid, url){
  document.getElementById(iframeid).src=url;
  return false;
}

and have
<a href="http://www.ask.com" onclick="return loadintoIframe('myframe', this.href)"

Actually I wouldn't. I would do

<a href="http://www.ask.com" target="myframe"

after adding name="myframe" to the frame

 and remove all the scripts
Avatar of CalDev
CalDev

ASKER

mplungjan: Sorry to be so long responding to your posts. I work Tuesday - Saturday so I was not able to access the site to test until I was back in the office.

In regard to your question about posting a link to the site, unfortunately I can not post a link to the site since it's a development server and not live to the public at this time. I am also restricted from posting the actual page code on this sever.

I tried replacing the function "loadintoIframe" with your version and then modified a link to match your version: <a href="http://www.ask.com" onclick="return loadintoIframe('myframe', this.href)" when I tried this the link directed me to the page that was supposed to be displayed in the iframe. Was there something else I was supposed to add to the link?

When I tried eliminating the "loadintoIframe" function and changed the link code to "<a href="http://www.ask.com" target="myframe"  and added name="myframe" to the frame to the iframe nothing happens. When I click on the link I don't get an error an FF looks like it's going to load the page in the iframe but nothing happens. Am I missing a step?

I even tried mixing the versions in different ways but still no luck.
This works for me when I
1. do not try to load google into a frame (they give the message: Error: uncaught exception: Not exactly Luddite, but we are frame-busting.)

2. remove "display:none" from the iframe!!!

<link rel="stylesheet" type="text/css" href="http://www.stunicholls.com/menu/pro_dropdown_3/pro_dropdown_3.css" />

<script src="http://www.stunicholls.com/menu/pro_dropdown_3/stuHover.js" type="text/javascript"></script>
<script>

function loadintoIframe(iframeid, url){
 document.getElementById(iframeid).src=url;
 return false;
}

</script>

<ul id="nav" style="z-index:200;">
	<li class="top"><a href="#" id="services" class="top_link"><span class="down">Search Engines</span></a>
		<ul class="sub">
			<li><a href="#" class="fly">Major</a>
                  <ul>
                    <li><b>Top 3</b></li>
                    <li><a href="http://www.msn.com"   onClick="return loadintoIframe('myframe',this.href)">msn</a></li>
                    <li><a href="http://www.yahoo.com" onClick="return loadintoIframe('myframe',this.href)">yahoo</a></li>
                    <li><a href="http://www.bing.com"  onClick="return loadintoIframe('myframe',this.href)">bing</a></li>
                    <li><b>other</b></li>
                    <li><a href="http://www.ask.com" onClick="return loadintoIframe('myframe',this.href)">ask</a></li>
                </ul>
            </li>
        </ul>    
	</li>
</ul>

<!--------------------  Dropdown menu code end ----------------------------->


<!--------------------  iFrame code begin ----------------------------->
<div style="height:2000px;">
<iframe id="myframe" src="" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" vspace="0" hspace="0" style="overflow:visible; height:100%; width:100%; border:0px; position:relative; left:-20px;"></iframe>
</div>

Open in new window

Avatar of CalDev

ASKER

OK here is where I'm at after many, many tests and configuring multiple ways I was able to finally get the iFrame links in the drop down menu to load page content in the iframe (both FF and IE). However I was never able to get the URL variable function to work again (not working in either browser).

Therefore I can no longer address the page with a URL variable attached and have it display page assigned in the URL variable in the iFrame. Are you saying you were able to accomplish this? This was a key feature of the code before and I need it to work in final version. Any ideas?
Sorry I missed that part
So you want to pass the iframe URL in the url of the main page. Do you need to highlight the passed url in the dropdown too?

Here is loading any passed url:

<script>

function loadintoIframe(iframeid, url){
 document.getElementById(iframeid).src=url;
 return false;
}

window.onload=function() {
  var passed = location.search;
  if (passed) loadintoIframe('myframe',unescape(location.search.split('=')[1]));
}

</script>

Open in new window

Avatar of CalDev

ASKER

OK I tried out the new code but I'm still getting mixed results (see test code below).

In IE test 100% success.

1. IE URL variable test = works loads in iFrame.
2. IE menu item link test = works, all links displayed in iFrame.

------------------------------------------------------------------------------
Fire Fox test - failure

1. FF URL variable test = nothing happens (FireBug reports no errors).
2. FF menu item link test = link from page is loaded as a direct link to the page but not in the iFrame as expected.
<!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 with menu test</title>
</head>

<body>


<!------------------- Drop down menu Begin -------------------->

<link rel="stylesheet" type="text/css" href="http://www.stunicholls.com/menu/pro_dropdown_3/pro_dropdown_3.css" />

<script src="http://www.stunicholls.com/menu/pro_dropdown_3/stuHover.js" type="text/javascript"></script>


<!------------------- Data Table Code Begin -------------------->

<script>

function loadintoIframe(iframeid, url){
 document.getElementById(iframeid).src=url;
 return false;
}

window.onload=function() {
  var passed = location.search;
  if (passed) loadintoIframe('myframe',unescape(location.search.split('=')[1]));
}

</script>


<!------------------- Data Table Code End -------------------->

<span class="preload1"></span>
<span class="preload2"></span>

<ul id="nav" style="z-index:200;">
	<li class="top"><a href="#" id="services" class="top_link"><span class="down">Shopping</span></a>
		<ul class="sub">
			<li><a href="#" class="fly">Stores</a>
                  <ul>
                    <li><b>Online Shopping</b></li>
                    <li><a href="http://www.amazon.com/" onClick="return loadintoIframe('myframe',this.href)">Amazon</a></li>
                    <li><a href="http://www.overstock.com/" onClick="return loadintoIframe('myframe',this.href)">Overstock</a></li>
                    <li><b>Physical Stores</b></li>
                    <li><a href="http://www.target.com/" onClick="return loadintoIframe('myframe',this.href)">Target</a></li>
                    <li><a href="http://www.sears.com/" onClick="return loadintoIframe('myframe',this.href)">Sears</a></li>
                </ul>
            </li>
        </ul>
	</li>
</ul>

<!------------------- Drop Down Menu End -------------------->


<div style="height:2000px;">
<iframe id="myframe" src="" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" vspace="0" hspace="0" style="overflow:visible; height:100%; width:100%; border:0px; position:relative; left:-20px;"></iframe>
</div>

</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
but NOT for some of the links since they break out of the iframe like google
Avatar of CalDev

ASKER

OK I tried out the latest version of the code and based on the results I'm getting in both IE and FF, even in the CMS I will be using, I'm getting successful results. For coders that find this question online this solution has been tested and is functional. Testing was limited to just current versions of IE and FF, since other browsers/versions on the site I'm developing are insignificant, your results for other browsers/versions may vary.

In IE test 100% success.

1. IE URL variable test = works loads in iFrame.
2. IE menu item link test = works, all links displayed in iFrame.

------------------------------------------------------------------------------
In FF test 100% success.

1. FF URL variable test = works loads in iFrame.
2. FF menu item link test = works, all links displayed in iFrame.

In regard to the sites that are busting out of the frame those were being used strictly for testing and the final version will only be loading pages I control so there should not be an issue with that.

mplungjan: Unless you have anything to add I'm going to close this question and award you the points. Great work!
Great. I do not have anything to add.
Good to see a test exit report :)
Perhaps we should hire you as a tester :)
Avatar of CalDev

ASKER

This is a greatly simplified solution from all others I found on the web with bloated code. The solution was tested and reliably functions in both the current versions of IE and FF and has the additional ability to pass a page to the iFrame using a URL variable.
Points to asker for clearly communicated question and well tested delivery :)
Avatar of CalDev

ASKER

Just wanted to add a note that I have since tested this solution with the current versions (as of 5/7/2010) of Chrome, Opera and Safari (PC) and all functioned correctly with all aspects of this script.
:) Great to hear.