Redirects page base on user country (Javascript) - 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"}
});

Related

Bypass log .js file

i want to bypass this login because i don't have the rest of the source /core
*sorry in advanced because i am a newbie i only know python and php :3
var lgnusr = $('#lgnusr'),lgnpss = $('#lgnpss');
$(document).ready(function(){
//---------------------------------------
$('.inpx input').on('focus',function(){
$(this).parent().addClass('animat');
}).on('blur',function(){
$(this).parent().removeClass('animat');
if($('.denger').hasClass('hide') === false){$('.denger').addClass('hide');}
}) ;
$('#signbtn').on('click',function(){
if(isempty(lgnusr)){lgnusr.parent().addClass('error');}else if(isempty(lgnpss)){lgnpss.parent().addClass('error');}else{
var obj = {user:lgnusr.val().trim(),pass:lgnpss.val().trim(),csrf:$(this).data('csrf'),rem:$('#remmber').prop('checked')};
$('.loading').removeClass('hide');
$.post("./core/",{qlogin:JSON.stringify(obj)},
function (data) {
$('.loading').addClass('hide');
if(data.ok === true){window.location.href = './dashboard'}else{$('.denger').removeClass('hide');}
},
"json"
).fail(function(){alert('Invalid Request!! Please Refresh The Page and Try Again..');});
}
});
//--------------------------------------
});```
you replace the fail action:
fail(function(){alert('Invalid Request!! Please Refresh The Page and Try Again..');});
by
fail(function(){ window.location.href = './dashboard';});
you type any password and any user

window.location issue on hubspot

window.location will redirect to external links like google, but won't redirect to a url with the same domain from where it originates (it'll link to sub-domains, though). This came from a hubspot form, customized to redirect the user to a specific thank-you page based on their inquiry type. This is all in wordpress. There is no issue defining the choice variable. I'm new to JavaScript, thanks for any help.
onFormSubmit: function($form) {
var choice = $('select[name="form_field_dropdown_select"]').val();
if (choice == 'Support') {
window.location = 'https://www.mycompany.com/support-thank-you/';
} else {
window.location = 'https://www.washingtonpost.com/';
}
}
I'm running this code through my console because I know the choice variable is set up correctly. The error I'm getting is an Uncaught SyntaxError: Unexpected token ( on the line onFormSubmit: function($form)
onFormSubmit: function($form) {
var choice = "Support";
if (choice == 'Support') {
window.location = 'https://newcloudnetworks.com/support-thank-you';
} else {
window.location = 'https://www.washingtonpost.com/';
}
}
Additionally, I can see the redirect initially going to the support page but then immediately redirecting to the default/home page.
[21/Mar/2019:11:13:44 -0600] "GET /support-thank-you HTTP/1.1" 200 6015
[21/Mar/2019:11:13:46 -0600] "GET / HTTP/1.1" 200 9683
The issue has been resolved. It was an issue I had to raise hell with to Hubspot and they were able to resolve. It had nothing to do with an error in the JS code. Thanks.
So, you need to provide more info, but I'll assume your:
hostname(domain): www.mycompany.com
So you can add another check for the domain the form was used.
const myDomain = 'www.mycompany.com';
onFormSubmit: function($form) {
var choice = $('select[name="form_field_dropdown_select"]').val();
if (choice == 'Support') {
let domain = window.location.hostname;
if (myDomain == domain) {
// change path if the form is used in your domain
window.location.pathname = 'support-thank-you';
} else {
// redirect to you domain
window.location = 'https://www.mycompany.com/support-thank-you/';
}
} else {
window.location = 'https://www.washingtonpost.com/';
}
}

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/

Javascript not redirecting

I have searched extensively for the javascript code to redirect to a different page on the same site and in the same folder and I have tried numerous things. I'm unable to get it to work in localhost or on the web-server. This is what I've tried:
<script type="text/javascript">
function confirm_submit()
{
var con = confirm("Submit form and print invoice?");
var url = 'dashboard.php?del=no';
if (con == true) {
document.location.href = url;
/*I have also tried:
window.location = 'dashboard.php?del=no';
window.location.replace("dashboard.php?del=no");
window.location.href = "dashboard.php?del=no";
*/
}
else {
return false;
}
}
</script>
This is the code on the button :
onclick="return(confirm_submit())"
From everything I've read this should work but it is not working. Can someone please tell me why it is not working and what I need to do to make it work? Thank you in advance.
EDIT: I have answered the question below. It's a workaround, but it is doing the job.
this will work without submitting your form
var url = window.location.href;
splitUrl = url.split("/");
splitUrl[splitUrl.length-1]="your page";
url = splitUrl.join("/");
window.location.href = url;
I have spent too much time on this issue so I have decided to go a different route. I'm using javascript to change the value of a hidden field and then using PHP to redirect to the other page as a result of the posted value of the hidden field. Thanks to all who have given their time to this matter on my behalf. Cheers
<script type="text/javascript">
function confirm_submit()
{
var con = confirm("Submit form and print invoice?");
if (con == true) {
document.getElementById('bool').value = true;
$("#edit_workorder").submit();
}
else {
return false;
}
}
</script>
And the PHP is:
if($bool == true) {
header("Location:dashboard.php?del=no");
exit;
}
It's doesn't really answer the question, but it works and that is more important to me right now.

Get Vimeo thumbnail for video using jQuery

I've found similar questions but none of the answers show clearly and easily how to get a thumbnail for a vimeo video using jQuery and JSON. If anyone can help that would be great, here is what I've got but it shows nothing at the moment.
var vimeoVideoID = '17631561';
var videoCallback = 'showThumb';
$.getJSON('http://www.vimeo.com/api/v2/video/' + vimeoVideoID + '.json?callback=' + videoCallback,
function(data){
$(".thumbs").attr('src',data[0].thumbnail_large);
});
Thanks in advance.
I believe you're having the "same origin policy" issue. You should consider writing a server side script using something like "file_get_contents" or "fopen", enabling you to grab the data from vimeo, translate it to json, and output to your javascript with a nice ajax call.
If you would like to avoid using a server-side script you may use the data type JSONP.
var vimeoVideoID = '17631561';
$.getJSON('https://www.vimeo.com/api/v2/video/' + vimeoVideoID + '.json?callback=?', {format: "json"}, function(data) {
$(".thumbs").attr('src', data[0].thumbnail_large);
});
Notice the URL is a bit different from how you are using it. The callback which you defined as a var is unnecessary. You're attaching the getJSON to a function directly, so you'll call the 'callback' in the url '?'. This informs the getJSON function to pass the successful data return to the supplied function.
You can test my code here. Hope it helps!
With the updated API, it would be...
$.getJSON('https://vimeo.com/api/oembed.json?url=https://vimeo.com/' + id, {format: "json"}, function(data) {
$(".thumbs").attr('src', data.thumbnail_url)
});
You can use this function which supports all types of Vimeo links & sizes:
function get_vimeo_thumbnail(url, size, callback)
{
var result;
if(result = url.match(/vimeo\.com.*[\\\/](\d+)/))
{
var video_id = result.pop();
if(size == 'small'){
var video_link = encodeURIComponent("https://vimeo.com/" + video_id + "?width=480&height=360");
$.getJSON('https://vimeo.com/api/oembed.json?url=' + video_link, function(data) {
if(data && data.thumbnail_url){
if (typeof(callback) !== 'undefined') {
callback(data.thumbnail_url);
}
}
});
}
else
{
$.getJSON('http://vimeo.com/api/v2/video/' + video_id + '.json', function(data) {
if(data){
if (typeof(callback) !== 'undefined') {
var thumbnail_src = data[0].thumbnail_large;
if(thumbnail_src){
callback(thumbnail_src);
}
}
}
});
}
}
}
To use it:
// Available sizes: large, small
get_vimeo_thumbnail('https://vimeo.com/475772381', 'large' function(url){
alert(url)
})
Please check out this page; Vimeo has a new method call oEmbed as Vimeo is now pushing it's new oEmbed technology.
The method above, will fail on IE (no thumbs will be shown).

Categories

Resources