Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing URL parameters to iframe

Tags:

I would like to grab the parameters from a page that contains an iframe, and append the parameters to the src of the iframe.

Example:

PAGE CONTAINING IFRAME--

http://www.example.com/pref.html?c=%433r3jutlut9se%23&e=test%40example.com

IFRAME IN THE ABOVE PAGE--

<iframe id="myiframe" name="myiframe" src="prefcontent.aspx" frameborder="0" width="100%" height="800"></iframe> 

Here's one of the many things I've tried, which I thought would work in theory, but I had no luck with it:

<script language="javascript"> $(function() {      var loc = window.location.toString(),     params = loc.split('?')[1],     params2 = loc.split('&')[2],     iframe = $('#myiframe');      console.log(params);      iframe.src = iframe.src + '?' + params + '&' + params2;   }); </script> 
like image 524
Nostalgia80 Avatar asked Oct 17 '12 21:10

Nostalgia80


People also ask

How do I pass a URL in a query string?

To pass in parameter values, simply append them to the query string at the end of the base URL. In the above example, the view parameter script name is viewParameter1.

How do I pass cookie value in URL?

1. Add a cookie with the key/value from the URL query string (parameter). 2. Pull the cookie value to use as a variable in a link.


2 Answers

if your using jQuery you might want to strip down your code up:

$(function() {     var search = window.location.search;     $("#myiframe").attr("src", $("#myiframe").attr("src")+search); }); 

Use window.location.search; as it will snip off the hash tag at the end of the url and is a lot faster as it is native code.

Then use jQuery selectors to find and replace the attribute of src on the iframe, with it's current src plus the search acquired from this page.

Hope this helps.

like image 171
lededje Avatar answered Nov 10 '22 01:11

lededje


You could append the querystring to the source of the iFrame

<script language="javascript">     var iframe = document.getElementById('myiframe');         iframe.src = iframe.src + window.location.search; </script> 
like image 27
adeneo Avatar answered Nov 10 '22 00:11

adeneo