Created
January 26, 2017 12:10
-
-
Save iredun/cca46985f2194e6b7fb7e380c923ec48 to your computer and use it in GitHub Desktop.
Target _new
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery('a[target^="_new"]').click(function() { | |
return openWindow(this.href); | |
} | |
function openWindow(url) { | |
if (window.innerWidth <= 640) { | |
// if width is smaller then 640px, create a temporary a elm that will open the link in new tab | |
var a = document.createElement('a'); | |
a.setAttribute("href", url); | |
a.setAttribute("target", "_blank"); | |
var dispatch = document.createEvent("HTMLEvents"); | |
dispatch.initEvent("click", true, true); | |
a.dispatchEvent(dispatch); | |
} | |
else { | |
var width = window.innerWidth * 0.66 ; | |
// define the height in | |
var height = width * window.innerHeight / window.innerWidth ; | |
// Ratio the hight to the width as the user screen ratio | |
window.open(url , 'newwindow', 'width=' + width + ', height=' + height + ', top=' + ((window.innerHeight - height) / 2) + ', left=' + ((window.innerWidth - width) / 2)); | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment