SELF CLOSE a WebView window JavaScript - javascript

Hello I am trying to close a WebView window automatically when the page code stop executing. I am adding this to the end of the page
<script type='text/javascript'>
self.close();
</script>
And if I open the webview by clicking on a link in messenger the code works as expected - closes the webview as soon as it gets to it. However, if I open the same page from a link inside an already opened webview, then the code is not triggered and page is not closed.
In both cases I am using one and the same page and the code is added to the bottom of the page. The other code of the page is PHP and HTML
Any idea what is the reason?
The only think that might be is if the webview actually open the link in a new "tab" (not sure if tabs exist in webview) and as a result js cannot close it. This is just a thought.

Related

JavaScript behavior change depending on how page is loaded

I am having an issue where seemingly the way JavaScript executes is changing depending on whether I open a file from a brand new browser tab (copy-pasting the link in) versus if I open the page by clicking a link from another page.
A summarized version of my code is:
<html>
<head>
<script>
try {
// ...other code...
throw new Error("error");
} catch (e) {
location.href =
"https:\/\/google.com"; // would be "https:\/\/mysite.com"
}
window.close();
</script>
</head>
</html>
If I open this in a brand new tab by copy-pasting the link, the tab will navigate to https://google.com
However, if I open the link via a hyperlink, the new tab closes immediately.
Ideally, I want the redirect to be successful regardless of how the page is loaded.
Would love to understand what's going on here as the above code snippet is from an external library that I can't modify
Appreciate any insight or help!
From MDN:
This method can only be called on windows that were opened by a script using the Window.open() method, or on top-level windows that have a single history entry. If the window doesn't match these requirements, an error similar to this one appears in the console: Scripts may not close windows that were not opened by script.
In other words, a page opened by clicking a link on another site cannot use window.close(). However, if you create it with window.open(), that restriction does not apply and you can use window.close(), which is overriding your redirect. Ctrl clicking on a link to open it in a new tab also satisfies this condition. You can verify that by running window.close() in the devtools console and seeing whether or not it closes the tab.

JavaScript Bookmarklet; click button, reload page, then open page

I am trying to create a bookmarklet that follows this process:
From any page, click the bookmarklet
load a specific page.
Trigger an input button which causes page to reload
Once reloaded; open a new page; in this case a social media page.
This is what I've written so far:
if (!($ = window.jQuery)) {
// Inject jQuery to make life easier
script = document.createElement( 'script' );
script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js';
script.onload=SocialCredentials;
document.body.appendChild(script);
}
else {
SocialCredentials();
}
function SocialCredentials(){
$('input:submit').trigger('click');
window.open('http://www.facebook.com', '_blank');
}
The button submit click causes the page to process and clear the credentials of the user so they can visit social sites.
The above code works, but I want to make sure the page finishes loading before opening a new social media page.
Do I need to add some kind of wait() function? If I do add a wait method, will it kill the JavaScript and not open the new window? I'm fairly new to JavaScript, so I'm not familiar with these types of mechanics.
Edit:I should note that I don't have access or control to the page this bookmarklet will work on so I can't alter it's flow. What I'm trying to do is more of a favor for another department as a quick fix until they can make changes on their end.
Edit2: Updated the process.
If a page reloads, any code currently running on that page, including code from a bookmarklet, is ended and removed. Traditionally bookmarklet code ceases to work after a page load and user clicks it again.
There are three workarounds that I know of.
A.) Change the process that loads the page to instead use AJAX.
B.) Change the process that loads the page to instead open a new window, and then use JavaScript to manipulate the new window.
C.) Before triggering the page load, open a new child window and insert code into it. The code in that child window can then monitor its parent and take actions on the parent even after the parent has reloaded.

inject javascript into page which loads in childbrowser phonegap

I have a link which opens in the child browser of phonegap - for those not familiar with phonegap just think of it as a new window.
The link that I am opening has an annoying alert which pops up the 1st time the webpage is loaded on the device. I would like to stop this alert poping up by trying to change the cookie the webpage uses to check if it has been loaded before.
Is this possible?
How would I go about doing this?
Little Difficult.
You could host your own html page remotely and enclose the page you are opening inside a iFrame which you could stop the alerts by some type of javascript. Or (using ajax) remotely retrieve the page you are after?

GWT: Cannot keep control of Window anymore once I close its pop-up

My app is:
Doing something on main window
Opening a new window to do something there
Window.open(userInfo.signInURLs.get("Google"), "Google", null);
Closing that new window
native public static void close()/*-{ $wnd.close(); }-*/;
Trying to reload the main window
Window.Location.reload();
Everything is fine until I close the new windows. I cannot keep control of the main window anymore. Actions I run are never executed. Any ideas?
If you work with GWT you want as faw page reload as possible. If you load data you make partial page reloads using ajax. The approach you have chosen is not very feasible. With window.location.reload - you make a whole page reload. Don't do that.
You should use a modal window - in gwt that is very simple.
Use DialogBox, that makes a modal Window.
http://examples.roughian.com/index.htm#Widgets~DialogBox

How to give control back to iframe window after it opens a popup

I have a question on javascript window management.
My application opens an iframe which displays a link. Clicking on the link, opens a popup window, in which I enter my login credentials. My intention is to close the popup window after the login credentials are entered. And this I have been able to achieve using a JS call to self.close().
There after my server side script does some more processing and would like to display the results back in the iframe. This is where things break for me.
The overall flow is as follows:
Iframe Displays a Link --> Clicking on the Link Pops up a window --> Popup Window closes after credentials are entered --> I see my original iframe now (How do I display the contents back in the iframe). Here is the code snippet that closes the popup.
5
6 self.close();
7
The parent of the popup is the iframe. If I modify the above script to give the focus back to its parent, which in my case will be the iframe, will that suffice? Or am I issing something here?
you can access the iframe from your popup by using opener. for example this will reload your iframe:
<script type="text/javascript">
opener.location.href = "htp://mypage.com/myiframe.php";
</script>
you just have to add the right parameters to show what you want to. (of course you have to do this before your self.close();)

Categories