Javascript not redirecting - javascript

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.

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

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"}
});

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/

How can I change a form submit action from displaying an alert popup to redirecting to a new page?

I'm trying to troubleshoot something someone else created (never a fun prospect) and want to eliminate an alert popup and have the user redirected to another page after form submission ... I've searched the web and found a couple of possible alternatives, but I also don't want to blow up the page, as I'm not all that experienced in PHP troubleshooting ... this is, I'm guessing, a painfully simple 101-level question, but many thanks in advance ... here's the relevant code:
jQuery("form#commentform input#submit").click(function(event) {
event.preventDefault();
var name = jQuery("form#commentform input#author").val();
if(name=="") {
alert("Name cannot be empty!");
jQuery("form#commentform input#author").focus();
return false;
}
var email = jQuery("form#commentform input#email").val();
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(#((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(#\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
var email_test= pattern.test(email);
if (email == "" || email_test == false) {
alert('Valid email address is required!');
jQuery("form#commentform input#email").focus();
return false;
}
var phone = jQuery("form#commentform input#url").val();
var comment = jQuery("form#commentform textarea#comment").val();
jQuery.get('http://theenergyclub.com/wp-content/themes/EnergyClub/formPost.php', {type:'guestpass', name:name, email:email, phone:phone, comment:comment, rnDnum:Math.random()}, function(data) {
alert('We have received your request. Thank you!');
You can try to replace the last line
alert('We have received your request. Thank you!');
with
window.location = "http://example.com/"
You should be able to just change alert('We have received your request. Thank you!'); to window.location = '<URL>'; where <URL> is the URL you want to re-direct the user to.

Categories

Resources