John
John

Reputation: 2501

is there any way to change urls is iframe?

I want to show a page of my website within iframe in another page. I mean you can see helper.html while you are navigation main.php. but I want to change some links in helper.html regarding to some conditions set in main.php.

The regular solution is to get content of helper.html and process it in main.php, then echo it. But it is server side, I want this process to be client side.

Is that possible with JavaScript?

Upvotes: 3

Views: 204

Answers (4)

Karolis
Karolis

Reputation: 9562

If those files are on different domains but you have a full control of them, then use the following solution:

  • In the main page call an iframe with GET parameters. For instance:

    <iframe src="foo.html?parameter=value" width="400" height="500"></iframe>
    
  • In an iframe parse GET parameters using Javascript and show an appropriate content:

    // get the current URL
    var url = window.location.toString();
    //get the parameters
    url.match(/\?(.+)$/);
    var params = RegExp.$1;
    // split up the query string and store in an
    // associative array
    var params = params.split("&");
    var queryStringList = {};
    
    for(var i=0;i<params.length;i++)
    {
        var tmp = params[i].split("=");
        queryStringList[tmp[0]] = unescape(tmp[1]);
    }
    
    // print all querystring in key value pairs
    for(var i in queryStringList)
        document.write(i+" = "+queryStringList[i]+"<br/>");
    

    Source: http://www.go4expert.com/forums/showthread.php?t=2163

Upvotes: 1

user961954
user961954

Reputation: 3214

Yes it is possible if both frames are in same domain. See sample function: in the help frame the id of link is assumed to be "link"

function ChangeLink()
{
    var Helpframe = document.getElementById("Helpframe");
    var innerDoc = Helpframe.contentDocument || Helpframe.contentWindow.document;
    var link = innerDoc.getElementById("link");
    link.href="http://www.google.com";
}

This solution is inspired from Javascript - Get element from within an iFrame

Upvotes: 1

Rob W
Rob W

Reputation: 349042

If your files are located at the same domain, you can use the top.frames property, ro refer to the window object of named frames:

Assume the top HTML to has such a structure:

<iframe name="main" /><iframe name="helper" />

Inside main:

top.frames["helper"].document.getElementById("linkID").href = "http://newlink.com";

If you're using AJAX, you can add the previously shown code in the callback handler. If main.php reloads on change, at the code within <script> tags.

Upvotes: 2

Pwaddles
Pwaddles

Reputation: 142

Try this...

var myIframe = document.getElementById('SomeIFrame'); 
myIframe.src = 'www.joobworld.com'; 

Upvotes: 1

Related Questions