tags:

views:

675

answers:

3

Is there any way to vary the initial content of an iframe depending on the referring page?

For example ‘page 1’ has an iframe displaying ‘initial content 1’ when page 1’s URL is typed directly in address bar. If a link is clicked on ‘page 2’ linking to ‘page 1’ iframe content on ‘page 1’ will start as ‘initial content 2’.

I hope that makes sense.

Thanks.

+1  A: 

You could solve it this way in Javascript using the document.referrer HTML DOM property:

if (document.referrer != 'page2.html') {
   document.getElementById('iFrameId').src = 'content1.html';
}
else {
   document.getElementById('iFrameId').src = 'content2.html';
}

Here is some documentation on the referrer property.

You could do it server-side too. Just tell us what environment (ASP.NET, PHP, ...) you are using.

splattne
link has changed, see: http://www.w3schools.com/jsref/prop_doc_referrer.asp
vsync
Thank you, vsync!
splattne
A: 

thanks very much splattne.

I have just tried your idea but i cant get it right. my page now has the following in the body:

"if (document.referrer != 'page2.html') { document.getElementById('prdfrm2').src = 'contenta.html'; }
if (document.referrer != 'page3.html') { document.getElementById('prdfrm2').src = 'contentb.html'; }
if (document.referrer != 'page4.html') { document.getElementById('prdfrm2').src = 'contentc.html'; } else { document.getElementById('prdfrm2').src = 'contentx.html'; } "

it seems that however i access the parent page (direct, from link on page 2,3 or 4) the same content loads, the content from 'contentc' - can you think why?

Thanks!

first use alert(document.referrer) to see the referrer.then try using a switch statement:switch(document.referrer){case 'page2.html': document.getElementById('prdfrm2').src = 'contenta.html'; break; case 'page3.html': // other content break;default: // else...}good luck :D
Shawn Simon
A: 

Thanks for the help guys but im still having problems. If this could be done 'server side' that would be great but i have very little experience with this. Does anyone know how to do it in php?

If poss i would like to vary the iframe content depending on the URL typed into the address bar. For example i have been using the script:

" function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { if (oldonload) { oldonload(); } func(); } } }

function gup( name ) { name = name.replace(/[[]/,"\[").replace(/[]]/,"\]"); var regexS = "[\?&]"+name+"=([^&#]*)"; var regex = new RegExp( regexS ); var results = regex.exec( window.location.href ); if( results == null ) return ""; else return results[1]; }

addLoadEvent(function() { var targetURL = gup("iframe"); document.all.myIframe.src=targetURL; })

"

In the address bar i can type "http://mysite.com/page11.html?&iframe=page12.html" so that the iframe on page 11 would contain page 12.

The prob is this script works in safari and IE but not FF :(

As I said, any tips on doing it in php would be fantastic.

Thanks! Matt