5

I debug mobile website, and it is more comfortable to do it with desktop emulator that with mobile device itself. I use emulator embedded in Google Chrome. It changes viewport size and user agent, so website looks reasonably similar to mobile phone.

When any link contains target="_blank" however Chrome opens it without any emulation, so it is impossible to debug websites that contain such links.

My question is: is there any way to force Google Chrome to open "new tab" links with the same emulation settings as its parent window?

Vitalii
  • 4,434
  • 4
  • 35
  • 77

1 Answers1

0

Perhabs you could create an if statement that checks if you are on an mobile device and then loop through all of your links and replace 'target="_blank"' with 'target="_self"'. I think thats fine because you only need this during the developement time on your pc.

if(/android|mobile/i.test(navigator.userAgent)){
   var links = document.getElementsByTagName("a");
   for(var i = 0; i < links.length; i++){
      links[i].setAttribute("target","_self");
   }
}
Nimmi
  • 153
  • 1
  • 13
  • It is good idea if you control web page that you debug with Chrome and hence you can inject JavaScript on that page. And if you do not have access to web server, probably it is possible to write Chrome extension that will embed this JavaScript automatically, because embedding JavaScript manually on each page load is time consuming. – Vitalii Nov 09 '15 at 18:00
  • @Vitaliy you could also just save this as bookmark and click on it to add the script: (javascript: if(/android|mobile/i.test(navigator.userAgent)){var links = document.getElementsByTagName("a");for(var i = 0; i < links.length; i++){links[i].setAttribute("target","_self");}}) – Nimmi Nov 09 '15 at 18:49
  • And will such bookmark work with `iframe` for example? – Vitalii Nov 10 '15 at 10:26
  • It depends on the iframe src. If the iframe is on the same url (same origin) as the site then you can replace "document.getElementsByTagName('a')" with "iframe.contentWindow.getElementsByTagName('a')" were iframe is a reference to the iframe elmement. If it isnt the same origin then there is no way to get it work. – Nimmi Nov 10 '15 at 16:54