问题描述:

I am trying to achieve something like, www.example.com/en/test/page.html will load the frameset of www.example.com/2.html.

Which I am able to do.

But now once 2.html is loaded in frameset I want to replace .html" to .html" target="_top".

So that all the links present in 2.html would be opened in the parent window instead of frameset itself.

<!DOCTYPE html">

<html lang='en' xml:lang='en' xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta charset="utf-8" />

<title>Test Page</title>

<script>

(function divert()

{

var urlString = parent.document.URL;

var a1 = new RegExp("/en/test/page");

if(a1.test(urlString)) {document.write('<frameset cols="100%" rows="100%"><frame src="http://www.example.com/2.html"></frameset>');}

else{document.write('<frameset cols="100%" rows="100%"><frame src="http://www.example.com/3.html"></frameset>');}

var str = document.getElementById("demo").innerHTML;

var res = str.replace('.html"', '.html" target="_top"');

document.getElementById("demo").innerHTML = res;

})();

</script>

</head>

<body onLoad="divert()"></body>

网友答案:

Hi @Vinod you can use target="_parent"if you have only 2 level of frame-set or just want to open the link to the parent frame only..

Or to open the link to the current page you can use target="_top"

You can read more about it here

UPDATE:

You can also use JavaScript to detect if page is loaded inside a frame and than add target to the links pragmatically..

You can use window.location.origin and parent.window.location.origin to detect if you are in the frame

And than you can use following code to add target using JavaScript

using jquery:

$(document).ready(function(){
  $('#link_other a').attr('target', '_blank');
});

without using jquery

window.onload = function(){
  var anchors = document.getElementById('link_other').getElementsByTagName('a');
  for (var i=0; i<anchors.length; i++){
    anchors[i].setAttribute('target', '_blank');
  }
}
网友答案:
<!DOCTYPE html">
<html lang='en' xml:lang='en' xmlns="http://www.w3.org/1999/xhtml">
    <head>
            <meta charset="utf-8" />
                    <title>Test Page</title>
                            <script> 
                            (function divert()
                                    {
                                    var urlString = parent.document.URL;
                                    var a1 = new RegExp("/en/test/page");

                                function targetUrl(){
                                 var links = document.getElementById("demo").contentDocument.documentElement.getElementsByTagName('a');
                                 for(var i =0; i<links.length; i++){
                                 links[i].target = '_parent';}
                                }

                                    if(a1.test(urlString)){document.write('<frameset cols="100%" rows="100%"><frame id="demo" src="http://www.example.com/2.html"></frameset>');}
                                    else{document.write('<frameset cols="100%" rows="100%"><frame id="demo" src="http://www.example.com/3.html"></frameset>');}
                                    document.getElementById("demo").onload= targetUrl;
                                    })();
                            </script>
    </head>
    <body></body>
</html>
相关阅读:
Top