The base tag causes the iframe to open as a new window in Internet Explorer

I have a problem with the base tag, which only affects Internet Explorer (versions 8, 9 and 10).

The following code is used to open dynamic content in an iframe and functions correctly in Chrome and Firefox. It also works correctly in Internet Explorer, but only without the <base target="_blank"/> . Enabling this tag causes the iframe to open as a new window (which makes sense, however, this is not what I'm trying to do.

 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <base target="_blank"/> </head> <body> <div id="main"></div> <script type="text/javascript"> function load_iframe(name, height, width) { var div = document.getElementById(name); var ifrm = document.createElement('iframe'); ifrm.id = 'iframe_' + name; ifrm.frameBorder = 0; ifrm.scrolling = 'no'; ifrm.noresize = 'noresize'; ifrm.marginheight = 0; ifrm.marginwidth = 0; if (height !== 0) { ifrm.height = height; } if (width !== 0) { ifrm.width = width; } div.appendChild(ifrm); content = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head></head><body></body></html>'; if (/msie/.test(navigator.userAgent.toLowerCase()) || window.opera) { ifrm.contentWindow.contents = content; return ifrm.src = 'javascript:window["contents"]'; } else { doc = ifrm.contentDocument; doc.open(); doc.write(content); doc.close(); return ifrm; } } load_iframe('main', 250, 300); </script> </body> </html> 

How can I fix this problem? Unfortunately, I could not get the code to work on the violin, perhaps because it relies on <base/> in <head> .

+6
source share
2 answers

I just deleted the /msie/.test(navigator.userAgent.toLowerCase()) || part /msie/.test(navigator.userAgent.toLowerCase()) || and worked fine on IE8. Are you sure you need this piece of code?

However, if you do not want to remove this part, you can remove the base tag and add it after by creating an iframe :

 load_iframe('main', 250, 300); //and then: var hd = document.getElementsByTagName("head")[0]; var bs = document.createElement("base"); bs.target= "_blank"; hd.appendChild(bs); 

I tested on chrome, IE8, IE11 and it works great!

+4
source

As stated in the comments target = "_ blank", the browser opens a new window or tab (depending on your configuration).

Just remove it or replace it yourself (tested on IE8):

 <base target="_self"/> 
+1
source

Source: https://habr.com/ru/post/959135/


All Articles