How can ı do if is true when my url address - javascript

Hello everyone ı want to control my url address in my code ı want to redirect when url its true
if (data.is_okey && **URL** == true) {
window.location.replace('https://google.com.tr');
}

The current address in your browser is available with the variable window.location.href
So you should write:
if (data.is_okey && window.location.href == "**SOME URL YOU'D LIKE TO BE TRUE**") {
window.location.replace('https://google.com.tr');
}

I see that you want to check if a url is equal to a unique/decided one?
For example;
let checkUrl = "/login"
let url = window.location.pathname;
if(data.is_okey && url != checkUrl) {
window.location.replace("/login")
}
If you want sth like this. Please try it or explain with more details.

Related

Redirects page base on user country (Javascript)

I’m struggling to find a good resource on how to execute this. I wanted to redirect the page base on user location. I know this required custom code but at the moment I’m stuck with it and most of the resources that I found are outdated. I’m using ipinfo.io to determine the user country.
Here’s the code that I’m using :
$.get(“https://ipinfo.io”, function(data) {
if(data.country !== “GB”){
console.log(“no gb”)
}else {
var test
test = ‘GB’
console.log(“THIS IS GB”)}
}, “jsonp”);
But then I’m stuck on how to use those value and redirect the page.
If anyone has any experience on this please guide me :pray:t3:
To redirect use location.href
$.get('https://ipinfo.io', function(data) {
if(data.country !== “GB”){
location.href = 'http://ADDRESS-TO-GB'
} else {
location.href = 'http://ADDRESS-TO-ANYWHERE'
}, 'jsonp');
$(function(){
$.get( "https://ipapi.co/json/", function( data ) {
switch(data.country){
case 'US':
window.location = 'http://www.google.us';
break;
case 'KR':
window.location = 'http://www.google.kr';
break;
default:
window.location = 'http://www.google.com';
break;
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script>
you can use a switch case to do redirection base on the callback received data.
try this one
$.getJSON('https://ipinfo.io', function(data) {
if(data.country == 'America'){
location = 'your_redirect_url';
return;
}else if(data.country == 'Beijing'){
location = 'your_redirect_url';
return;
}
})
you can use $.getJSON function then location.href to redirect.
$.getJSON( "https://ipapi.co/json/", function( data ) {
if(data.country !== "GB"){ location.href="your site url"}
if(data.country !== "US"){location.href="your site url"}
});

redirect a page from URL jquery

I want to redirect from a url. It works when I use #!, but I want to work with a parameter other than this.
//works
url = 'localhost/#!/about'
if (document.location.hash.replace(/^#?!\//, '') != '') {
loadResource(true);
}
//doesn't work
url = 'localhost/about'
if(location.pathname.replace(/^\/([^\/]*).*$/, '$1') != ''){
loadResource(true);
}
Why? Is there a way to not use #!?
The code above doesn't show any code for redirection. What does loadResource do?
Anyways, redirect in javascript is done like this:
// Syntax: document.location.href = url;
document.location.href = 'http://localhost/about';

How to redirect to the base URL if the given URL ends with a slash?

I have this link http://localhost:3007/sellers-toolkit/, but the right page is only the one without a trailing slash. So I would like a redirection to http://localhost:3007/sellers-toolkit.
I tried to write this part in JavaScript, but it just makes the page reload and nothing happens. What is the right way to achieve this behavior?
var url = "http://localhost:3007/sellers-toolkit/";
if (url) {
url = "http://localhost:3007/sellers-toolkit";
window.location.href = url;
}
my solution for this issue is that i was trying to do that on the javascript side but i found that i have to do this on my node server side.
i used this code:
if (req.url == '/sellers-toolkit' || req.url == '/sellers-toolkit/' ) {
category = 'Selling Privately';
}
Try like below:
var url = "http://localhost:3007/sellers-toolkit/";
var spe_char_last =url.substr(url.length - 1);
if(spe_char_last == '/')
{
url = url.slice(0, -1);
if (url) {
url = "http://localhost:3007/sellers-toolkit";
window.location.href = url;
}
}

Checking a URL in an if/else statement?

I'm trying to check a submitted URL (excluding any additional paths).
The user submitted URL is:
var inputUrl = document.getElementById("inputVal").value;
If the user submits 'http://stackoverflow.com/questions/ask' then I'm trying to create an if/else statement that will determine whether the site is 'stackoverflow.com', regardless of 'http/https' or any '/.../.../' after .com
if (inputUrl == "stackoverflow.com") {
console.log ("stackoverflow");
} else {
console.log("not stackoverflow");
}
Any help would be greatly appreciated.
if(inputUrl.toLowerCase().indexOf("stackoverflow.com") > -1) {
...
}
A little trick to have the browser do most stuff for you (can be found at MDN):
var url = document.createElement('a');
url.href = 'http://stackoverflow.com/questions/ask';
console.log(url.host); // stackoverflow.com
if (url.host == "stackoverflow.com") {
console.log ("stackoverflow");
} else {
console.log("not stackoverflow");
}
The same way you can also access other parts of the URL like protocol or hash.
$("button").on("click",function(){
var inputUrl = document.getElementById("inputVal").value;
inputUrl=inputUrl.split("http://")[1] || inputUrl.split("https://")[1] ;
inputUrl=inputUrl.split("/")[0];
if (inputUrl == "stackoverflow.com") {
console.log ("stackoverflow");
} else {
console.log("not stackoverflow");
}
}) ;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input id="inputVal" value="http://stackoverflow.com/questions/ask">
<button>
submit
</button>
jsfiddle Demo : https://jsfiddle.net/geogeorge/h40dvbq6/2/

Window.Open causing issue when URL doesnot contain http

I have a textbox where the user can enter URL and button named Check link. On clicking button, a new window should be opened with the URL entered by the user. If i enter URL as "http://google.com" then window.open('http://google.com'); is working fine but if i enter "www.google.com" then it is appending to the current window url (http://localhost:1234www.google.com) which is not a valid url. How to make this work?
Thanks in advance
Check & prepend
if (!/^(http:|https:)/i.test(url))
url = "http://" + url;
try this:
function f(url) {
if (url.indexOf("http://") == -1) {
url = "http://" + url;
}
window.open(url);
}
If your question is "How do I test if a string starts with 'http://' and if not prepend it?" then something like this:
var url = "somestring.com"; // as set from your textbox
if (0 != url.toLowerCase().indexOf("http"))
url = "http://" + url;
It would be wise to trim leading and trailing spaces before doing this test, but I'll leave that as an exercise for the reader...
Add a onclick button function and handle the text in that.
function checkLink() {
var url = document.getElementById("textbox-id").value;
if(url.substr(0,7) != "http://") {
alert("Invalid URL! Please make it like http://myurl.com");
return false;
}
}

Categories

Resources