Update
User Quentin helped me solve it. The lightbox had a event.stopPropagation(); in the function which prevented the eventlistener from bubbling up. After removing it the links worked properly.
I used this comment with honyovk's addition to replace the stopPropagation with a more elegant function: https://stackoverflow.com/a/1089622/3461722
Question
I have a webapp for iOS in which I use an EventListener to prevent links from opening in Safari. This works flawless for 99% of the links, but a few specific links still open in Safari for some reason unknown to me.
This is my Javascript:
// Mobile Safari in standalone mode
if(("standalone" in window.navigator) && window.navigator.standalone){
// If you want to prevent remote links in standalone web apps opening Mobile Safari, change 'remotes' to true
var noddy, remotes = false;
document.addEventListener('click', function(event) {
noddy = event.target;
// Bubble up until we hit link or top HTML element. Warning: BODY element is not compulsory so better to stop on HTML
while(noddy.nodeName !== "A" && noddy.nodeName !== "HTML") {
noddy = noddy.parentNode;
}
if('href' in noddy && noddy.href.indexOf('http') !== -1 && (noddy.href.indexOf(document.location.host) !== -1 || remotes))
{
event.preventDefault();
document.location.href = noddy.href;
}
},false);
}
The links that fail to open in the webapp are nothing special, and other links on the page work without problem. An example of a not properly working link:
Buy
This link is opened in a lightbox, but on other pages I have similar links also in a lightbox and they work fine. I have several popups with roughly the same link, only the "green" is different and some have an extra "&time=1", they all fail to open in the webapp.
Is there someone who can find fault with this code?
Related
I have tried a lot of things for onbeforeunload and came up with a solution-
window.addEventListener("beforeunload", function(e) {
(e || window.event).returnValue = null;
return null;
});
This seems to work when i navigate away from the current page inside the application, but its not working for which it is intended that is on browser close. i want this to work only on browser close and not on navigating by clicking on other links inside the application. Any leads will be appretiated. Thanks in advance.
This question already has answers here:
Open external links in a new tab without jQuery
(4 answers)
Closed 5 years ago.
I use the following code so everything opens in the web app:
<script type="text/javascript">
var a=document.getElementsByTagName("a");
for(var i=0;i<a.length;i++)
{
a[i].onclick=function()
{
window.location=this.getAttribute("href");
return false
}
}
However, even on desktop, links that have target="_blank", still open in the same tab. How can I make all external links open in a new tab, while still keeping all internal links in the same tab. I've found solutions for single links, but I have too many to change by hand.
Note: I'm on apache so a config file change would also work.
If you set the "target" attribute on the links they will open in a new tab:
<script>
var origin=window.location.origin;
var a=document.getElementsByTagName("a");
for(var i=0;i<a.length;i++)
{
var link = a[i];
if(link.href && link.href.indexOf(origin)!=0)
link.setAttribute("target", "_blank");
}
</script>
In WordPress admin you can go to Theme editor and put this code in your footer.
I managed to find an answer from this post.
I just put this in my header:
<script>
// Mobile Safari in standalone mode
if(("standalone" in window.navigator) && window.navigator.standalone){
// If you want to prevent remote links in standalone web apps opening Mobile Safari, change 'remotes' to true
var noddy, remotes = false;
document.addEventListener('click', function(event) {
noddy = event.target;
// Bubble up until we hit link or top HTML element. Warning: BODY element is not compulsory so better to stop on HTML
while(noddy.nodeName !== "A" && noddy.nodeName !== "HTML") {
noddy = noddy.parentNode;
}
if('href' in noddy && noddy.href.indexOf('http') !== -1 && (noddy.href.indexOf(document.location.host) !== -1 || remotes))
{
event.preventDefault();
document.location.href = noddy.href;
}
},false);
}
</script>
and all normal links open within the web app, but on mobile and desktop, links with target="_blank" open in a new tab.
I created a jquery mobile web app and on one of the buttons it links to an .HTML file. When I add the web app to my home screen and tap that button it opens the .HTML page in safari instead of staying within the app. I did some research online and came across this file but I still can't get it to work. Any ideas how to address this?
https://github.com/mrmoses/jQuery.stayInWebApp
Here's what I use...applies to all anchors. To make a long story short, it's disabling each anchors default behaviour, grabbing the clicked anchor's href, and then using js to open the link within the web-app. Applied to mobile devices only.
<script>
$(function() {
// prevent anchor links from opening in safari, web-app fix
if (("standalone" in window.navigator) && window.navigator.standalone) {
// For iOS Apps
$('a').on('touchend click', function (e) {
e.preventDefault(); return false;
var new_location = $(this).attr('href');
if (new_location != undefined && new_location.substr(0, 1) != '#' && $(this).attr('data-method') == undefined) {
window.location = new_location;
}
});
}
});
</script>
I have implemented a script to prevent the link in my mobile app on my ipad.
It works fine but I have problem now with the popup I have with jquery mobile.
The problem is when I use this script, the popup window doesn´t open anymore.
What can I do to open the popup window?
The script:
(function(document,navigator,standalone) {
// prevents links from apps from oppening in mobile safari
// this javascript must be the first script in your <head>
if ((standalone in navigator) && navigator[standalone]) {
var curnode, location=document.location, stop=/^(a|html)$/i;
document.addEventListener('click', function(e) {
curnode=e.target;
while (!(stop).test(curnode.nodeName)) {
curnode=curnode.parentNode;
}
// Condidions to do this only on links to your own app
// if you want all links, use if('href' in curnode) instead.
if('href' in curnode && ( curnode.href.indexOf('http') ||
~curnode.href.indexOf(location.host) ) ) {
e.preventDefault();
location.href = curnode.href;
}
},false);
}
})(document,window.navigator,'standalone');
In this case, you need to open it programmatically.
$('#popupID').popup('open');
Solved it...
what i have done:
instad to use the script i have write above, i only use this code in the .
<a onclick="parent.location='root/example.html'" id="ex"></a>
this allows me when i see my app in the fullscreen mode.. to navigate between the pages without to open it in the browser, the page loaded in my app.
I'm developing for a site that does something a bit strange with some links.
When the user clicks on a link, an indicator GIF that looks kinda like this appears next to the link, and the link itself is disabled. It then navigates to the page. This is because of concerns that impatient users might repeatedly click the link if it doesn't load immediately, putting an extra load on our server.
Here's how we do this:
<a id="link1" href="/target_page"
onclick="var link = document.getElementById('link1');
var loc = link.href;
link.removeAttribute('href');
link.setAttribute('onclick', 'return false;');
document.getElementById('link1_img').style.display='';
window.location = loc;">
Link Text
<img id="link1_img" src="/images/indicator.gif" style="display:none;" />
</a>
Here's the problem. While this has the expected behavior if the user clicks on the link normally, or right-clicks it and opens it in a new window or tab from the context menu, it doesn't always work properly if the user middle-clicks or Ctrl+clicks on the link.
The desired behavior in that case would be to skip all the JavaScript stuff and simply open the link in a new tab. I did a quick test on Windows with the latest version of each major browser, and IE, Firefox, and Opera all do this. Chrome and Safari, however, display the indicator and open the link in the current tab.
Any suggestions on how to make it behave consistently on all browsers?
You can use the code here for handling middle clicks, and take a look here for the Ctrl+click. However, I found that I had to use .mousedown for middle clicks instead of clicks (at least in Firefox).
If you use
$('#link').click(function(e) {
if(e.ctrlKey && e.which == 1 || e.which == 2) { // Ctrl + Click
console.log("hi");
$(this).css('color','#aaa');
$(this).click(function(e) { e.preventDefault(); });
}
});
and don't add e.preventDefault(), it should work as expected. Works for me in Firefox, Chrome, and IE9.
Edit: Without jQuery, this is how I would do it. Do take a look at this post about preventDefault() vs return false;, though.
document.querySelector('#link').onclick = function(e) {
this.style.color = '#aaa';
disableLink(this);
}
// middle clicks only captured in mousedown
document.querySelector('#link').onmousedown = function(e) {
this.style.color = '#aaa';
if (e.button == 1) {
disableLink(this);
}
}
function disableLink(elem) {
elem.onclick = function(e) { console.log("HI"); this.href = '#'; return false;}
elem.onmousedown = function(e) { console.log("HI"); this.href = '#'; return false;}
}
Unfortunately, there seems to be a difference in the way Chrome and Firefox handle middle clicks. Firefox only shows middle clicks in onmousedown, and it opens a new tab during the onmousedown. This means that for Firefox, we have to disable the link after onmousedown. However, in Chrome, the middle click shows up in onmousedown and onclick, but the link is only opened after the onclick. Since we disable the link on the mousedown, the onclick never gets a chance to run :(.
Not sure how to fix this...