Prevent website from loading in WebView and open in browser - javascript

When my website shared in Facebook messenger and such apps, it opens into a WebView.
My questions are:
Is there a way to force it to open in browser instead of the WebView?
As I see target="_blank" urls also loaded into WebView, is there a way to leave the WebView with hyperlink or JavaScript?

You can verify the UserAgent string on your server, to decide whether you should load the website or reject the request. You can refer to this SO to get insight of the UserAgent for Webview. However, app can easily by pass this check by setting the UserAgentString in Webview. This approach atleast helps you to black list some of the Webviews.
To conclude, there is no guaranteed way with which you can control whether Website will be loaded by WebBrowser or Webview.

No, you can't! why don't you built a custom script to manually detect whether web page is loaded into webView?
you can do following 2 things
if ur site is loaded into webView you can prompt "target='_blank'" to the user or manually request external browser to load that page.
if($_SERVER['HTTP_X_REQUESTED_WITH'] == "com.facebook.orca")
echo 'var isWebView=true;';
else
echo 'var isWebView=false;';

Related

How can I judge my html is loaded by mobile phone's browser or app's webView, or pc browser?

I have a html5 page, it can be loaded by mobile phone's browser, pc browser, and my app's webView, in there how can I judge my html page is loaded by which of the upper three different situations?
EDIT
I mainly want to know if the html is loaded by my app's webView.
The html has a button, which can provide a method, but can access the database, if I use my app's webView to load the html, I will pass the user-id to the method, because there is no cookie here, if I loaded the html by mobile phone's browser and pc's browser, there is cookie for saving the user-id, I will not pass user-id to the html.

How to detect URL scheme and prevent the default navigation to that URL in phonegap hybrid application

I have the following requirement for my application (Android, iOS):
When the application launches it displays a login.html page (which is part of the application). After logging in, the application's webview should be occupied with home.jsp from an external domain. When user clicks on logout button in home.jsp it has to navigate back to login page . On click of logout when we usewindow.location.href="login.html" then it tries to find the page on xxx domain.
Is there a way to detect this navigation URL and override the URL from javascript or phonegap properties in the application?
When I inspect window.location.href in an android emulator I get file:///android_asset/www/index.html
But I think Nathans idea of moving it to the server is a good one. You could also have one on the device if you really need to. (PErhaps you should ask the person specifiying the app achitecture how they would do it :) )
The answer is going to vary depending on how you've implemented the mentioned WebView where home.jsp is being displayed in. You did not provide any code or any specific information so the answer is going to be the same - somewhat vague...
If you've opened a new WebView, then you can't control it from JavaScript. You'll need to control it via Java or Objective-C code (you did not mention which environment you're developing for...).
For example, if you'll look in the your-app\android\native\src\com\your-app\your-app.java file, you'll see how the native layer loads the application's index.html file after the Worklight JavaScript framework has been loaded.
Similarly, you could re-use this approach in your own application to close and re-load login.html.
If you're in fact doing the mentioned re-direct from the comments, meaning you're re-using the current webview but replacing its content with external content, then I think it is expected that you've lost the context of the application, and when looking for login.html - it doesn't find it... because you've moved from app-context to web-context. They do not know each other.
I think you should not do this re-direct. Instead, you need to open a new WebView using a Cordova plug-in, and in this new WebView to display your external content.
In this Overlayed WebView, you can detect any urls that are clicked on and if the sign-out URL was detected, then close the WebView.
You can see parts of this in action in the Integrating server-generated pages in hybrid applications tutorial and accompanying sample project.
In the sample project, you can see the functions provided (where you can add yours) in android\nativeResources\src\com\IncludeExternalPages\IncludeExternalPages.java.

make an iframe loaded in app display mobile site?

I have an app made with Phonegap.
For a certain feature, sometimes I need to load a webpage/ site into the app - for this, i use an iframe.
The iframe always displays the desktop version of the webpage though, even when I know the page has a responsive design.
Is there a way to force the iframe to load the site as a mobile?
You should use the InAppBrowser plugin to open up an external web page. This is going to be much safer since the foreign page won't have access to your Cordova instance. (If I have a third party page, and you load my page into your app through an iFrame, I can easily "hack" your application and steal user data, run arbitrary Cordova commands, etc.)
Additionally, when using the InAppBrowser, you can tell the browser to open any links in the current Cordova WebView, so that external site will just appear in your app. I don't know what your external site looks like, but this might be what you want to do. You can accomplish this by setting the target as _self, like this:
var ref = window.open('http://apache.org', '_blank', 'location=yes');

how to navigate user from my facebook application's canvas page to my web page?

I am working on a rails application. I have implemented facebook integration in my application. User can invite his friends to my application. Invitation system is working fine but problem is when any friend of user clicks on request it gets redirected to my facebook applications canvas page. I want that the user be redirected to my web applications landing page instead of in the application's canvas page. I tried to do so by including javascript in page which opens in canvas on facebook.
<script>
window.location.href = "http://www.google.com";
</script>
But it is giving me error as following.
Refused to display document because display forbidden by X-Frame-Options.
How to redirect user to my applications landing page as soon as it land son my canvas page? I want to achieve something similar to pinterest does.
Any help is highly appreciated.Thank you
Your frame doesn't have access to the window object, as that'd let you access Facebook's pages and do nasty stuff to them. You need to do top.location.href instead of window.location.href.
Your facebook application is probably sending the X-Frame headers, preventing Facebook to include your application iside the iFrame in the first place.
Some applications include that to prevent clickjacking, see here for an explanation of how it works:
http://blog.mozilla.com/security/2010/09/08/x-frame-options/
You might want to disable that header and then try again.

window.open doesn't open in same session

I'm using the below anchor tag on a JSP page to open another page from the same application, but the new window is not opened in the same session and instead it redirects to the login page of my application. Any clues why?
<a href="#" onclick="window.open('/path_to_same_page', '_blank',
'toolbar=0,status=0,resizable=1'); return false;">Click here...</a>
Try this workaround, not sure it will help but worth a shot:
Click here...
By having this, the window won't get opened by script initially, but rather by the target attribute.
Reason behind this behaviour is, the parent page is hosted on a IE web browser control embedded in our windows application. When it creates a new window (either using window.open or target="_blank"), the new window is owned by iexplore.exe process and doesn't inherit the session cookies from the parent IE window, which is owned by our application process. There is no generic solution to this problem. In our case, we used some kind of Single Sign On to share the session context between two window instances.
You stated in your answer that
the parent page is hosted on a IE web browser control embedded in our windows application
There actually is a solution to this problem. Your application needs to handle the NewWindow2 event in order to maintain the session across windows.
Refer to the following MSDN resources for details on this:
How to use the WebBrowser control NewWindow2 event in Visual C#
How to use the WebBrowser control NewWindow2 event in Visual Basic .NET
How To Use the WebBrowser Control NewWindow2 Event (for Visual Basic 5.0 and Visual C++ 5.0)
first encode that url with encodeURL(""); and then add in javascript file

Categories

Resources