Redirect Safari To Chrome - javascript

I'm trying to redirect user's of my mobile webapp to use Chrome rather than Safari. I tried using the following:
<script>
javascript:location.href="googlechrome"+location.href.substring(4);
</script>
However this constantly opens tabs in Chrome in a loop. Do you know how I can successfully do this?
Cheers,
Dara

This will cause the page to open every time the webpage is loaded, regardless if you are in Safari or Chrome. This is also very poor User Experience to just forward the user to another browser without their input.
It would be better to have some way for the user to open your site in Chrome and also to have an explanation why it is needed.
There are other schemes for https and callbacks: https://developer.chrome.com/multidevice/ios/links#uri_schemes
<p>This webapp is best viewed in Google Chrome</p>
<button type="button" onclick="openInChrome()">Open in Chrome</button>
<script>
var openInChrome = function() {
if (/^((?!Chrome).)*(Safari)+.*$/.test(navigator.userAgent)) {
var protocol = location.href.substring(0,location.href.indexOf(':'));
var url = location.href.substring(location.href.indexOf(':'));
if (protocol === "http") {
location.href = "googlechrome" + url;
}
else {
location.href = "googlechromes" + url;
}
}
}
</script>
Edit:
Added a check to verify they are in Safari.

Well, the reason is pretty obvious; Chrome is instructed to open Chrome too. You just want a userAgent conditional.
if (navigator.userAgent.indexOf("CriOS") == -1) {
location.href="googlechrome"+location.href.substring(4);
}
I would go on my standard rant about user agent checking being bad, but I trust what you're saying about this being a private webapp. Since iOS doesn't let you change your default browser, I guess this is a fair workaround.

Related

How to crash Chrome browser?

For testing purposes, I need a quick and easy way to crash my tab/browser. I'm stress testing my website, and these sites are invaluable for Google Chrome.
chrome://crash
chrome://kill
chrome://hang
chrome://quit
chrome://restart
These sites can be found at chrome://about. Another site (not listed on chrome://about is chrome://inducebrowsercrashforrealz- it will crash the top-level process for Chrome, including all tabs in all browser windows and all opened apps). Originally, I was trying to see what data was preserved in case of a crash. For example, the textarea in our form remained filled out upon restarting.
However, these URL's are only valuable if I manually type the URL into the browser. Using this question (and many others I don't have the links to), I confirmed what I suspected- you can't
redirect to chrome:// pages
link to chrome:// pages
in any other way request info from chrome:// pages (ie. iframe)
Link to chrome:// url from a webpage
This poses a problem- I want to cause the browser to crash (chrome://inducebrowsercrashforrealz) when the user reaches a specific part of the form. Is there any script which can cause the browser to crash?
I've tried while loops, but even when they're exponential, they take a while to kill the page, and never affect the browser. I'd like the crash to be either instantaneous, or eat up enough memory for the browser to freeze.
WARNING this will crash ANY browser or the computer using PopUps attack:
If you want to crash somebodies computer or browser
PopUp CODE that CRASHES a browser or computer
<script type="text/javascript">
function CrashAndBurn(url) { popupWindow = window.open( url,'popUpWindow','height=181,width=666,left=3,top=222') }
</script>
<body onload="JavaScript:CrashAndBurn(self.location,'_blank');JavaScript:CrashAndBurn(self.location,'_blank');">
If you really want to make their browser seriously hang up, you use this popup it will continue to uploading with 2 more and 2 more popup pages for each popup that means 2x2 popups second time 4 popups then 8, 16, 32, 64 and so on popups. It makes as if was a virus it will spread PopUps in the browser, filling the screen with popups till either the computer or the browser crashes and hangs up and shuts down, just...
NOTE: You did not hear this from me OKAY... LOL... I ONLY advice this as a piratical joke on a friend and not for an online web page.
If you wish to make it crash faster then just add more JavaScript:CrashAndBurn(self.location,'_blank'); to onload body like this example x4:
<body onload="JavaScript:CrashAndBurn(self.location,'_blank');JavaScript:CrashAndBurn(self.location,'_blank');JavaScript:CrashAndBurn(self.location,'_blank');JavaScript:CrashAndBurn(self.location,'_blank');">
It will load 4 popups for each popup that emans after 4 popups it will load 4x4 meaning 18 popups and then 18x4 that is 72 and then 72x4 that is 288 popups and then 288x4 that is 1152 popups just in 4 rounds of popups in a few seconds.
And if you want to be EXTREME HORRIBLE then use x10 example:
<body onload="JavaScript:CrashAndBurn(self.location,'_blank');JavaScript:CrashAndBurn(self.location,'_blank');JavaScript:CrashAndBurn(self.location,'_blank');JavaScript:CrashAndBurn(self.location,'_blank');JavaScript:CrashAndBurn(self.location,'_blank');JavaScript:CrashAndBurn(self.location,'_blank');JavaScript:CrashAndBurn(self.location,'_blank');JavaScript:CrashAndBurn(self.location,'_blank');JavaScript:CrashAndBurn(self.location,'_blank');JavaScript:CrashAndBurn(self.location,'_blank');">
and you have 8 popups with 10 that is 10x10=100 popups and then 1000 and after that 1 000 000 popups and GUARANTEED crash fast.
EDIT You can use a while loop instead of copy-pasting 10 times. It would be much shorter.
Google Chrome Crashers Websites List
chrome://badcastcrash
chrome://inducebrowsercrashforrealz
chrome://crash
chrome://crashdump
chrome://kill
chrome://hang
chrome://shorthang
chrome://gpuclean
chrome://gpucrash
chrome://gpuhang
chrome://memory-exhaust
chrome://memory-pressure-critical
chrome://memory-pressure-moderate
chrome://ppapiflashcrash
chrome://ppapiflashhang
chrome://quit
chrome://restart
<!DOCTYPE html>
<html>
<body>
<h2>Crashing Now</h2>
<p>Hit Ok To Crash</p>
<p id="demo"></p>
<script>
onbeforeunload = function(){localStorage.x=1};
if(confirm("Do you REALLY want me to crash your browser?")){
setTimeout(function(){
while(1)location.reload(1)
}, 1000)
}
</script>
</body>
</html>
for(var i = 0; i === i; i++) {}
should crash your current window
there is one way...
while(true){
var dp = document.getElementByID("spamr");
if(!document.getElementByID("spamr")){
var dp = document.createElementByID("spamr");
dp.innerHTML += "🤍🤍🤍🤍🤍🤍🤍🤍🤍🤍";
}
dp.innerHTML += "🤍🤍🤍🤍🤍🤍🤍🤍🤍🤍🤍";
}
for(var i = 0; i === i; i++) {}
This works, but slowly, it will make the browser unresponsive immediately though.
try {
window.location.replace("chrome://quit")
//some old browsers support this, if they are useing VERY old chromium.
} finally {
history.pushState(null, document.title, location.href);
window.addEventListener('popstate', function(event) {
history.pushState(null, document.title, location.href);
})
while(true) {
let location = 1
let item = 1
localStorage.setItem(location, item)
location = location+1
item = item*2
localStorage.getItem(location)
console.log("added and used: "+item+" at "+location)
}
}
if (window.closed) {
window.location.replace("run.html")
}
Works in chrome, edge, IE:
while(1)location.reload(1)
Continuously hard reload current page (works best over http/https)
And to prevent user from closing tab:
onbeforeunload = () => true; //only works if the user has interacted with the page
Recap:
onbeforeunload = () => true;
if(confirm("Do you REALLY want to crash your browser?")){
setTimeout(function(){
while(1)location.reload(1)
}, 1000)
}
WARNING: THIS WILL COMPLETELY AND UTTERLY CRASH ANY INSTANCES OF CHROME and, if you're running on chromebook or any old OS, this will completely crash your entire system!
EDIT:
a version that works for safari:
window.onload=()=>location.hash="M"+"\u0001".repeat(2**25)+"m";
I don't think there's any way to actually crash a client's browser via JS. However, it's possible to just perform tons of useless calculations rendering a tab useless.
while(true) {
for(let i = 99; i === i; i *= i) {
console.log(i);
};
};
for(let index = 0; true; index++){
console.log(index)
}
it will work slowly but it will work.
Simple bookmarklet that crashes your chromebook. If you try to shut down your chromebook and open it again, the screen will stay on for 5 seconds.
javascript:while (true) { window.location.reload(true); };
This should work!
<button onclick="crash()">???</button>
<script type="text/javascript">
function crash(){
if(navigator.userAgent.indexOf("Chrome") != -1 )
{
alert('WHoops')
window.open('http://crashchrome.com/', '_self', '');
}
else {
alert('Hehe')
while (1==1){
console.log(3234567654*45671234567890987654321)
}
}
}
</script>
This link:
http://a/%%30%30
will crash chrome because it results in a null character. Try it!

location.href in IE & Edge

Having issues getting my one page web application to work in Internet Explorer and Edge. It works perfectly fine in other browsers.
The issue that I'm having is that I can't seem to get the navigation to work.
I've tried the following:
location.href = '#quickQuiz'
location.href = '/#quickQuiz'
window.location.href = '#quickQuiz'
window.location.href = '/#quickQuiz'
document.location.href = '/#quickQuiz'
document.location.href = '#quickQuiz'
function goHere(where) { window.location = where; return false; }
location.hash = '#quickQuiz'
location.hash = '/#quickQuiz'
All of them works perfectly fine on other browsers. What am I doing wrong?
http://www.snabbteori.se if you wanna see it for yourself.
EDIT1: Additional code
The item in my navigation menu looks like the following:
<li data-icon="info"><a id="teoriNav">Teori</a></li>
And then I check for a click event on it. I know that it's possible to just put href="#teori in there, but I am just using this one as an example, there are other links where I need to perform AJAX calls too, this one doesn't need it though just to verify there isn't something wrong with my AJAX calls causing it.
$(document).delegate('#teoriNav', 'click', function () {
location.hash('#teori');');
});
I've also tried this but doesn't work:
$('#teoriNav').click( function() {
location.hash('#teori');
});
EDIT2: Updates
I believe I've fixed all jQuery errors and some other issues. The only thing I'm getting in the Internet Explorer console is:
HTML1300: Navigation occured.
DOM7011: The code on this page disabled back and forward caching.
And then when I click on the button I get output which I wrote which confirms it registers my click on the button. But still it won't navigate.
EDIT3: Ugly temporary fix
It works after doing a page reload after navigating.
var isIE = /*#cc_on!#*/false || !!document.documentMode;
var isEdge = !isIE && !!window.StyleMedia;
if(isEdge || isIE)
location.reload();
But I hate the fact that the site has to reload, and if someone has a real fix for this I would really appreciate it...
Edge has an issue with links if your code is not propper. You should check your console for any errors, This is most likely.
We will need more information from your side to help you further.
Post your HTML and your Javascript (just relating to this area of problem)
and we can then better help you.
I sagest fixing your jQuery errors first and trying again.
Ok my Browser test have given me a variation of 30-60 error relating to jQuery across Mac to Win. These will need to be addressed.
You also have a XSS issue that needs to be resolved. You have HTTPS from facebook but then HTTP on your website, These will need to match.

iOS9: Try to open app via scheme if possible, or redirect to app store otherwise

My question is about iOS9 only!
I have an HTML landing page, and I try to redirect the user to my app via URL scheme if the app is installed, or redirect to the Appstore otherwise.
My code is:
document.addEventListener("DOMContentLoaded", function(event) {
var body = document.getElementsByTagName('body')[0];
body.onclick = function () {
openApp();
};
});
var timeout;
function preventPopup() {
clearTimeout(timeout);
timeout = null;
window.removeEventListener('pagehide', preventPopup);
}
function openApp(appInstanceId, platform) {
window.addEventListener('pagehide', preventPopup);
document.addEventListener('pagehide', preventPopup);
// create iframe
var iframe = document.createElement("iframe");
document.body.appendChild(iframe);
iframe.setAttribute("style", "display:none;");
iframe.src = 'myscheme://launch?var=val';
var timeoutTime = 1000;
timeout = setTimeout(function () {
document.location = 'https://itunes.apple.com/app/my-app';
}, timeoutTime);
}
The problem is that the iframe trick doesn't work in Safari iOS9.
Any idea why?
My iframe trick based on this answer.
The iframe trick no longer works -- my guess is that Apple knows it will encourage more developers to implement Universal Links, more quickly.
You can still set window.location='your-uri-scheme://'; and fallback to the App Store after 500ms. There is a "dance" between popups if you take this approach, as we do at Branch (we do as a fallback if Universal Links don't work).
window.location = 'your-uri-scheme://'; // will result in error message if app not installed
setTimeout(function() {
// Link to the App Store should go here -- only fires if deep link fails
window.location = "https://itunes.apple.com/us/app/myapp/id123456789?ls=1&mt=8";
}, 500);
I wish I had a better answer for you. iOS 9 is definitely more limited.
For a helpful overview of what's needed for Universal Links should you go that route, check out my answer here or read this tutorial
As already mentioned setting window.location on iOS 9 still works. However, this brings up an Open in App dialog. I've put an example on https://bartt.me/openapp that:
Launches Twitter when the Open in Twitter app is clicked.
Falls back to the Twitter app in the App Store.
Redirects to Twitter or the App Store without the user selecting Open in the Open in App dialog.
Works in all browsers on iOS and Android.
Look at the source of https://lab.bartt.me/openapp for more information.
Maybe try giving you app support to Universal Links
Idea:
Avoid custom (JavaScript, iframe) solutions in Safari, replace you code with a supported Universal Link.
Example
<html>
<head>
...
</head>
<body>
<div class"app-banner-style">
In app open
</div>
...content
</body>
</html>
if you app support Universal Links (e.g. yourdomain.com), you muss configure your domain (and path) and iOS9 should be react to it link opening you App. That is only theory, but I guess should be work :)
https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/AppSearch/UniversalLinks.html#//apple_ref/doc/uid/TP40016308-CH12
iframe hack doesn't work in ios9 anymore. Possible solution is use two buttons.
Example:
$('#goToStoreBtn').text( "go to store" ).click(function(event){
event.preventDefault();
event.stopPropagation();
window.location = storeUrl; // https://itunes.apple.com/...
});
$('#goToAppBtn').text( "go to app" ).click(function(event){
event.preventDefault();
event.stopPropagation();
window.location = appUrl; // myApp://...
});
This is relatively old thread, but I have created a library that supports deeplinking on most of the modern mobile browsers. But this requires separate deeplinking page which needs to be hosted in different domain to support universal linking in ios9 facebook browser.
https://github.com/prabeengiri/DeepLinkingToNativeApp

Chrome not supporting my javascript

I just want, when a user comes to my site and closes the window, then I want to know the reason for leaving from my site.
So I'm sending the user to a survey page, using the following script.
It works in every browser but not in Chrome
<script type="text/javascript">
window.onbeforeunload = confirmExit;
function confirmExit() {
if (location.href.indexOf("index.php") != -1)
{
location.href = "http://www.test.com/survey.php";
return "Press 'cancel to go 'survey'";
}
}
</script>
Have you tried to use window.location = ("http://www.test.com/survey.php"); or window.open ("http://www.test.com/survey.php"); instead? That may be easier to do.
You can also do window.open ("http://www.test.com/survey.php", '_newtab'); This will make users less upset off when they are forced to redirect after leaving, because it will be in a new tab.
Well, sometimes there is a good reason for such a code user310850 is quoting
not all websites are in Web, some of them are internal corporate websites
Some if not most of big companies still use IE 6 as standard browser
I would use unload event handler. I assume jquery is good
$(window).unload(function() {
//your code here
});

A Firefox javascript bookmarking problem

I'm using the following JavaScript code:
<script language="JavaScript1.2" type="text/javascript">
function CreateBookmarkLink(title, url) {
if (window.sidebar) {
window.sidebar.addPanel(title, url,"");
} else if( window.external ) {
window.external.AddFavorite( url, title); }
else if(window.opera && window.print) {
return true; }
}
</script>
This will create a bookmark for Firefox and IE. But the link for Firefox will show up in the sidepanel of the browser, instead of being displayed in the main screen. I personally find this very annoying and am looking for a better solution. It is of course possible to edit the bookmark manually to have it not show up in the side panel, but that requires extra steps. I just want to be able to have people bookmark a page (that has a lot of GET information in the URL which is used to build a certain scheme) the easy way.
I'm afraid that it might not be possible to have Firefox present the page in the main screen at all (as Googling this subject resulted in practically nothing worth using), but I might have missed something. If anyone has an idea if this is possible, or if there's a workaround, I'd love to hear about it.
For Firefox no need to set any JavaScript for the bookmark an page by script, only an anchor tag with title and rel="sidebar" can do this functionality
Bookmark This Page
I have tested it on FF9 and its working fine.
When you click on the link, Firefox will open an dialog box New Bookmark and if you wish to not load this bookmark on side bar then un-check Load this bookmark in the sidebar from dialog box.
I think that's the only solution for Firefox... I have a better function for that action, it works even for Opera and shows a message for other "unsupported" browsers.
<script type="text/javascript">
function addBookmark(url,name){
if(window.sidebar && window.sidebar.addPanel) {
window.sidebar.addPanel(name,url,''); //obsolete from FF 23.
} else if(window.opera && window.print) {
var e=document.createElement('a');
e.setAttribute('href',url);
e.setAttribute('title',name);
e.setAttribute('rel','sidebar');
e.click();
} else if(window.external) {
try {
window.external.AddFavorite(url,name);
}
catch(e){}
}
else
alert("To add our website to your bookmarks use CTRL+D on Windows and Linux and Command+D on the Mac.");
}
</script>
You have a special case for
if (window.sidebar)
and then a branch for 'else' - wouldn't firefox land in the first branch and hence only add the panel?
Hojou,
It seems that is the only way to add a bookmark for Firefox. So FF needs to land in the first branch to have anything happening at all. I Googled some more but I'm really getting the idea this is impossible to properly address in FF...

Categories