开发者

is there any way to change urls is iframe?

开发者 https://www.devze.com 2023-04-10 05:48 出处:网络
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.

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 开发者_Python百科side, I want this process to be client side.

Is that possible with JavaScript?


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.


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


Try this...

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号