Getting sessionStorage.getItem(...).focus is not a function error - javascript

I have the following code,
var newTab;
var url = uniqueUrlEveryTime;
if (!sessionStorage.getItem(url)) {
newTab = window.open(url, '_blank');
sessionStorage.setItem(url, newTab);
newTab.focus();
}
else {
sessionStorage.getItem(url).focus();
}
When the Url is present in the sessionStorage, I am trying to get the newTab object from sessionStorage and set its focus, but it is giving me an error.
What am I doing wrong?

You set the session storage to be the url of the tab. Now you need to run through your tabs and check if the one of them has that url and then set .focus()
Some theoretical code below because I am not sure what your html for the tabs looks like
const tabUrl = sessionStorage.getItem(url)
if(tabUrl === newTab.url){newTab.focus() }

Related

how to refresh the page with original URL when URL has been changed via history.pushState()

I have used history.pushState() and now if the user refreshes the page then it is refreshing current URL which is not an original URL.
I tried detecting refresh page with a cookie, hidden filed but it is not working.
window.onload = function() {
document.cookie="PR=0";
var read_cookies = document.cookie;
var s = read_cookies;
s = s.substring(0, s.indexOf(';'));
if( s.includes("1"))
{
window.location.href = "https://www.google.com";
}
else{
document.cookie="PR=1";
}
loadURL();
};
function loadURL()
{
document.cookie="PR=1";
document.getElementById("visited").value="1";
var str="abc/b cd";
str=str.replace(/ /g, "-");
history.pushState({},"",str);
}
when user is refreshing the page I need original URL on that time.
This might be helpful. But you need control over the pushed url.
// this goes to your 'original' url
window.addEventListener('beforeunload', function (event) {
sessionStorage.setItem('lastPage', window.location.href)
}
// page with your pushed url
if (sessionStorage.getItem('lastPage') === 'PAGE_YOU_DONT_WANT_TO_BE_REACHABLE_DIRECTLY') {
window.location = 'PAGE_YOU_WANT'
}
I'm interested what the use case for this is. As far as my knowledge goes you can't suppress the refresh event completely.

How to get Chrome to throw exception when changing url if it fails

I have a reference to a new window opened with js
var theNewTab="";
theNewTab = window.open(theURL, 'winRef');
then I change the url in the as the user clicks on another link in the parent window using
theNewTab.location.href = targetLink;
theNewTab.focus();
The problem i'm having with chrome that id doesn't throw exception if the the window doesn't exist anymore "closed" unlink FF & IE which im using to open the window again.
try {
theNewTab.location.href = targetLink;
theNewTab.focus();
}catch(err) {
theNewTab = window.open(theURL, 'winRef');
theNewTab.focus();
}
PS: I tried to use "window.open" every time but if the window already open, id does not reload the page or it does but it doesn't re-execute the script I have in document ready I think.
I'm not sure what you need.
<script type="text/javascript">
var theNewTab = null;
function openNewTab(theURL) {
if (theNewTab == null || theNewTab.closed == true) {
theNewTab = window.open(theURL);
} else {
theNewTab.location.href = theURL;
}
theNewTab.focus();
};
// use the function when you need it
$('a').click(function() {
openNewTab($(this).attr('href'));
});
</script>
Is this example helpful for you?

Close Twitter Oauth login popup and redirect with callback to parent

I'm implementing the Twitter oauth with Java in a website and I have a problem with the Login popup.
I open the authorization URL in a Popup window and when the user finish with the login proccess, Twitter redirects to the Callback URL in the Popup. I need to know, if it is possible to close the Popup and redirects to the callback URL in the parent page.
Finally I found a solution.
I posted in Twitter developer forums as well.
This is what I did:
I set a timer with a function that is running every second.
In that function I check the popup URL, while I'm out of my domain i can't but when the callBack URL is launched, the window come back to my domain I read de URL and if i can find the oauth_verifier as a field in the URL i close the popup and read the URL to recover the verifier.
function loginTwitter(){
var f = document.forms["MainForm"];
var url = "https://api.twitter.com/oauth/authorize?oauth_token=***********";
var myWindow = window.open(url,"","resizable=yes,width=600,height=400,toolbar=no,titlebar=no,menubar=no,scrollbars=yes");
var timer = setInterval(checkWindow, 1000);
function checkWindow() {
try {
var ur = myWindow.location.href;
if (ur.indexOf('oauth_verifier') != -1){
var verifier = "";
var token = "";
clearInterval(timer);
myWindow.close();
ur = ur.substring(ur.indexOf('?')+1);
var urPartes = ur.split('&');
for (i = 0; i < urPartes.length; i++){
if (urPartes[i].indexOf('oauth_verifier') != -1){
verifier = urPartes[i].split('=')[1];
}
if (urPartes[i].indexOf('oauth_token') != -1){
token = urPartes[i].split('=')[1];
}
}
f.oauth_verifier.value = verifier;
f.oauth_token.value = token;
//here an AJAX call is done with the params in the form to do the final verification against twitter i nthe server
}
} catch (e){
} //end catch
}//end function checkWindow
}//end function loginTwitter
the try/catch block it is to avoid JS errors for "myWindow.location.href" while I'm trying to access the URL while the popup it is out of my domain.
I know it is not a very good way to do it but i couldn't find anything useful.

showModalDialog does not open PDF in IE8

I am using window.showModalDialog for opening a Pdf file it works in every browser but not working in IE8..it gives an empty modal...
here`s my function
function OpenHippaAgrementStaff(HIPAAFile) {
var mylink = "HipaaDoc/" + HIPAAFile;
window.showModalDialog(mylink, self, 'status:no;resizable:yes;help:no;scroll:no;width:1000;height:600');
return false;}
why dont you try by giving full URL like below,
function OpenHippaAgrementStaff(HIPAAFile) {
var mylink = "FULL_URL_HERE_WHERE_THE_PDF_IS_LOCATED/" + HIPAAFile;
window.showModalDialog(mylink, self, 'status:no;resizable:yes;help:no;scroll:no;width:1000;height:600');
return false;}
use document.location object to get full url

script to redirect if the url contains a querystring

SHORT: Need a script to remove the parameters on URL IF EXIST. Must be jquery/Script/
LONG: I have a site where I send traffic to with lots of parameters. These are processed instantly with php to cookies. After my cookie set I would like to refresh the same page but WITHOUT parameters. I cannot do it php since the cookies need to be processed by an iframe so I need a jquery/script to do it AFTER cookies are set via the iframe.
Thanks guys! :)
The below code will remove the query string parameter and will reload the page
window.location.href = window.location.protocol+"//"+window.location.hostname+window.location.pathname
The search attribute of window location describes the GET parameters on the url so -
if (window.location.search != "") {
window.location.search = "";
}
You may want to check out this question for a another solution.
And have a look at the MDN Documentation on window.location for more info.
I think you need to use something like:
var url_update= false;
var stripped_url;
function removeparametersFromURL(url_string) {
if (url_string.indexOf("#") > 1) {
URL = url_string.split("#")[0];
url_update= true;
}
if (url_string.indexOf("?") > 1) {
URL = url_string.split("?")[0];
url_update= true;
}
return URL;
}
var stripped_url = removeparametersFromURL(top.location.href);
if(url_update){
top.location.href = stripped_url;
}
setTimeout(function(){
stripped_url = removeparametersFromURL(top.location.href);
if(url_update){
top.location.href = stripped_url;
}
},5000); //will call function after 5 seconds

Categories