1

For example I have some link in index.html:

<a href="message.html">Go to message page</a>

In other page, iframe.html I have one iframe:

<iframe id="iframe" src="profile.html" frameborder="0" scrolling="no"></iframe>

Can it be done so that, when I click 'Go to message page' link, it redirect me to iframe.html and change iframe src to message.html?

4
  • 2
    If you can, do it at server-side
    – Alexander
    Sep 6, 2012 at 6:09
  • @Defense: See my answer below with a simple solution. Sep 6, 2012 at 6:43
  • @Alexander and Defense: What about the solution as mentioned below in my answer? Sep 6, 2012 at 7:05
  • @A.K, sorry man but people think JS is generally available when the truth is the opposite, that's why this should be done at server-side or in the same page
    – Alexander
    Sep 6, 2012 at 8:54

5 Answers 5

2

A simple Solution:

your link in index.html should look like this:

<a href="iframe.html?message.html">Go to message page</a>

In iframe.html do something like this:

$(function () {
    var url = window.location.href;
    var QueryStr = url.split('?')[1];    
    $('#iframe').attr('src', QueryStr);
});

Hope this will be your answer. Thanks!

SEE DEMO

See pages:

0
1

send your target page in query string like this

  $('a[href="message.html"]').click(function (e) {
        e.preventDefault()
        window.location = 'iframe.html?src="message.html"';
  })

on iframe.html do this

$(function () {
    $('#iframe').attr('src', getParameterByName('src'));
});

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

Supposed you have an iframe with id = iframeid, then

document.getElementById("iframeid").src = "new/address/here";
0

If you really want to do redirection in your client-side..

In your message.html file, put this:

<script type="text/javascript">
 window.location = "iframe.html"
</script>

Then add on your iframe.html:

document.getElementById("iframe").src = "message.html";

Haven't tested it, but the steps that you said might cause and infinite loop of redirection.

0

you can change your link tag like this

<a href="iframe.html?ifsrc=message.html">Go to message page</a> 

and in iframe.html page onload functin write this script

//---------------- within head tag----------of iframe.html--------
    function GET() {
       var data = [];
        for(x = 0; x < arguments.length; ++x)
                data.push(location.href.match(new RegExp("/\?".concat(arguments[x],"=","([^\n&]*)")))[1])
                return data;
        }

        //--- on iframe.html onload function
    function iframeOnload(){

    document.getElementById("iframe").src =  GET("id")[0];

    }

//---------------- within head tag----------of iframe.html--------

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.