Link to home
Start Free TrialLog in
Avatar of wesmanbigmig
wesmanbigmig

asked on

Load iFrame content using a string in a URL

Hi Experts

I would like to know how to link to a webpage (Page A) from another page (Page B) and load that page (Page A) PLUS load specific content from Page C that is shown in an iFrame on Page A.

I'm thinking of using for example a URL string, eg something like...

www.url.com.au/default.asp?contentID=702&iframe=http://www.url.com

but I don't know how to pass the value for an iframe in a URL.

Does anyone out there know how to do this?
Avatar of siliconbrit
siliconbrit


I am not sure what you mean by "pass the value for an iframe in a URL".  Your syntax will already pass the value of your target iframe in the URL, assuming that www.url.com is your 'Page C'.

I think what you are asking is how to set the source of the iframe to be www.url.com.  You might do this by specifying the src (source) attribute of the iframe when you build the page:

   <iframe src="http://www.url.com" name="myframe" ></iframe>

Depending on the server-side language you are using, you would set this attribute using the variable 'iframe' available in the $_GET array.  Here is an example in PHP:

      <iframe src="<?php echo $_GET["iframe"]; ?> name="myframe" ></iframe>

Note that you don't have to include the 'http://' syntax since you could pre-specify this in your code.  Some malware scanners block the string 'http://' if it appears anywhere in a URL other than the beginning of the string.  So you could:

      <iframe src="http://<?php echo $_GET["iframe"]; ?> name="myframe" ></iframe>

    ...and your URL would be: www.url.com.au/default.asp?contentID=702&iframe=www.url.com

Once again, I am not sure if this is your question, so post back if this is not any help.  There are also other ways to achiee this, so it might be worthwhile expanding on your question.



   <iframe src="http://www.url.com" name="myframe" ></iframe>;

Open in new window

Avatar of wesmanbigmig

ASKER

Hi - thanks for your advice. However, I can't use PHP, as I'm on a Windows server, so any suggestions in Javascript or ASP would be great.

And yes you are correct in saying "I think what you are asking is how to set the source of the iframe to be www.url.com.  You might do this by specifying the src (source) attribute of the iframe when you build the page:"

...that is what I am trying to do. Set the contents of the iFrame that is contained in a page using a URL that is passed from another page.

Thanks
ASP
<iframe src="<%request.querystring('iframe')%>"></iframe>


OK, assuming your URL is: www.url.com.au/default.asp?contentID=702&iframe=www.url.com

<iframe border=1 src="http://<% Response.Write(Request.QueryString("iframe")) %>" name="myframe" ></iframe>
Hi siliconbrit and QPR. Thanks for your suggestions. These would work well if the CMS software I am using accepted ASP, but it doesn't seem to like it. So, the solution has to be in JavaScript.

So, can we do what you have done here, but in JavaScript instead of ASP:

ie...how to do the below in JavaScript...

OK, assuming your URL is: www.url.com.au/default.asp?contentID=702&iframe=www.url.com

<iframe border=1 src="http://<% Response.Write(Request.QueryString("iframe")) %>" name="myframe" ></iframe>

OK, well I guess the difficulty is going to be with your CMS, and what Javascript events can be fired legitimately, and what information is available from the URL.  Without knowing more about the CMS, I can only give you some basic help.

In javascript, you can dynamically set the source file of your iframe as long as the iframe has a name:

   <iframe name="myIframe"></iframe>

The code to set the iframe source can be:

   frames['myIframe'].location.href='http://www.url.com'

You could also use the Javascript DOM directly, although this may not work in all browsers:

   document.all.myIframe.src='www.url.com'

OK so how do you get the parameter from the URL in Javascript.  Well it's a little tricky, but there is a function below to help you:



Using the gup function, what you need to do in your javascript is something like:

   var targetURL = gup("iframe")

   document.all.myIframe.src=targetURL

Rather than give you finished code here, I've given you the tools you need to complete the work, given the contraints of your CMS system.

I hope this helps.
/* Credit: Original code from http://www.netlobo.com */
 
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];
}

Open in new window

Thanks siliconbrit, so...

1) If I had an iframe on the page that was being called, and made sure this was called something like  <iframe name="myIframe"></iframe>, and
2) I included the gup function/script on the page being called, and
3) used var targetURL = gup("iframe") and frames['myIframe'].location.href=targetURL

would that work do you think? My javascript isn't very good, so any extra help would be greatly appreciated and I would be happy to award the full 500 points to you - I just want to get this working. Thanks again!
ASKER CERTIFIED SOLUTION
Avatar of siliconbrit
siliconbrit

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
Superb, this looks like it will work a treat - thanks siliconbrit! I'll give it a go and then award thr 500 points to you. Many thanks!
Hi Siliconbrit - your solution worked a charm! Thank you!