How to check window.location exist or not - javascript

My current code is working when location is found. But I want to make a check that if location is not found, it will redirect somewhere else.
Here is my code:
<script type="text/javascript">
var userAgent = window.navigator.userAgent;
if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i)) {
window.location = "theMYApp://"
}
else
{
}
</script>
I want to check if window.location = "theMYApp://" does not exist

If you want to check whether window.location object is existed on a web browser, you do not check. window.location object is always existed.
If you want to check text of 'theMYApp://' in window.location object, you can use below codes.
if (window.location.indexOf('theMYApp://') > -1) {
// theMYApp:// string is found in url
}

Related

URL check only works on second time within iframe

I currently use
<script src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
if (window.location.href.indexOf("Startseiteplayerrun") > -1) {
alert("You have the player open already !");
window.history.back();
} else {
//window.open("Startseiteplayerrun.html","_self");
link = document.createElement("a");
link.href = "Startseiteplayerrun.html";
//link.target = "_blank";
link.click()
;}});
</script><br>
home
to check whether the string "Startseiteplayerrun" is present in the url bar.
However,
this check only seems to happen after failing the first time
=> I'm able to open the frame once within itself:
Can this be mitigated ?
The purposeof this "if else" is to not allow another iframe,
if the urlbar contains "Startseiteplayerrun" ...
If the string won't check right away, check if framed
~FIN~
<script>
if ( window.location !== window.parent.location ) {
alert("You have the player open already !");
window.history.back();
} else {
// The page is not in an iframe
}
</script>

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

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() }

How to check in # in url?

Sometimes because of cache I add # in my url, like this:
http://www.example.com/#lang=3201954253
What I need is to check if there is #lang in url and remove it if present.
you can clear the hash.
window.location.hash = '';
or you can even use history api History Api. history.pushState and replaceState
history.replaceState() operates exactly like history.pushState() except that replaceState() modifies the current history entry instead of creating a new one.
window.history.replaceState( {} , 'foo', '/foo' );
You may try like this:
if(window.location.hash) {
// code
} else {
// code
}
or you may try this:
<script type="text/javascript">
if (location.href.indexOf("#") != -1) {
//code
}
</script>
If you want to remove it then you may try this:
window.location.hash = ''
On a side note:
You may try
window.location.href.split('#')[0]
to remove anything after # without refreshing your page.
if (window.location.hash.indexOf("lang")) {
window.location.hash = "";
}
var tel = window.location.href;
if(tel.indexOf("#") > -1){
alert("found");
} else {
alert('not found')
}

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

How to check if current URL ends in a specific pattern

How do I check if the current URL ends in: #!/about or #!/ask using JavaScript?
window.location.hash contains the current hash code, so basically:
if(window.location.hash === '#!/about') {
// Do something
} else if(window.location.hash === '#!/ask') {
// Do something else
}
You can use it however you need to.
Edit: As zzzzBov points out, if you need some jQuery, put it in a $(document).ready handler. ;)
To fetch the current url using jQuery
$(location).attr('href');
Here's the code snippet
jQuery(document).ready(function() {
var url=$(location).attr('href');
var match1 = arr.match('#!/about');
var match2 = arr.match('#!/ask');
if(match1){
alert("match1 found");
} else if{
alert("match2 found");
}
});

Categories