By using a ANCHOR tag , I am trying to redirect to http://google.com via HREF but at the same time I am posting some parameters to my another page via AJAX.Redirection is working fine but the parameters posting request is getting aborted.
Here is my code
<script type="text/javascript">
$(function(){
$('#c').click(function(){
$.post("mypage.php?param1=abc1212",function(data){
});
});
return false;
});
</script>
</head>
<body>
<a id="c" href="http://google.com" class='test'> Click 2 Call</a>
Now page perfectly move to google but the POST request which is mypage.php?param1=abc1212 is getting aborted.I dont know why?
I can see status = aborted in firebugI have searched alot but didn't get cluePlease Guide me about that why this problem is occurring and what is the solution?
What you need is preventDefault.
$(function(){
$('#c').click(function(event){
event.preventDefault();
$.post("mypage.php?param1=abc1212",function(data){
window.location.href = "http://google.com";
});
});
return false;
});
preventDefault stops the anchor from redirecting when clicked so that you can do your ajax.
Once your ajax is done then redirect to google.
<script type="text/javascript">
$(function(){
$('#c').click(function(){
$.post("mypage.php?param1=abc1212",function(data){
});
});
window.location.href = "http://google.com";
return false;
});
</script>
</head>
<body>
<a id="c" href="#" class='test'> Click 2 Call</a>
Related
I'm just trying to set a button to redirect to home page, but the page is just refreshing and not redirecting to the home page.
<script type="text/javascript">
function backs(){
location.replace("index.php");
}
</script>
Use location.href instead:
<script type="text/javascript">
function backs(){
location.href = "index.php";
}
</script>
in your button add the onClick event as following :
<button onClick="javascript:window.location.href='index.php">Edit</button>
Can you share the button code? Both the HTML and JS? I'm curious how you are calling the backs() function.
Also, as others suggested, location.href would be more appropriate.
How to navigate to previous browser url on button click?
Already tried this suggestions:
https://developer.mozilla.org/en-US/docs/Web/API/History_API
Onclick javascript to make browser go back to previous page?
Problem is that with window.history.go(-1) it navigates to homepage '/' url not to the previous page url.
Code:
<a class="link-terminal" href="" onclick="goToPreviousPage(); return false;"></a>
<script>
function goToPreviousPage() {
window.history.go(-1);
}
</script>
<head>
</head>
<body>
<a class="link-terminal" href="" onclick="goToPreviousPage(); return false;">go back</a>
<script>
function goToPreviousPage() {
window.history.go(-1);
}
</script>
</body>
Be sure you add the return false; part in your onclick.
This should fix your problem.
Also, in your code //window.history.go(-1)) has one too many ")" at the end.
How to detect what page i am being redirected to using javascript.
$(window).on("onbeforeunload",()=>{
alert("Something")
})
This code never executes (despite me reloading the page or clicking on other URLs). I am running my scripts on localhost. Also, i would like to know the URL of the page that i am being redirected to.
This is my full HTML:
<!DOCTYPE html>
<html>
<head>
<title>Practice</title>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
</head>
<body>
<script>
$(window).unload(()=>{
alert("Something");
})
</script>
Link
</body>
</html>
Maybe you can alert() or do whatever you want and then redirect to the url like the following example?
$('a').on('click', function(e){
e.preventDefault();
let url = this.href;
alert(`You're leaving this page, would be redirected to : ${url}`)
window.location.href = url;
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Link1
Link2
In modern browsers(IE8+, FF3.6+, Chrome), you can just listen to the hashchange event on window.
if ("onhashchange" in window) {
alert("The browser supports the hashchange event!");
}
function locationHashChanged() {
if (location.hash === "#somecoolfeature") {
somecoolfeature();
}
}
window.onhashchange = locationHashChanged;
I have a page for redirect purpose, I want to redirect user to another page once it hits this page:
<!DOCTYPE html>
<html>
<head>
<title>Open App</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
alert("test"); //this shows up.
$('#openApp').click(); //this button is not clicked.
});
</script>
</head>
<body>
redirect
</body>
</html>
I want the redirect happen once this page is load. But the click function is not fired. I have to click the link by myself to redirect to google.com.
I normally wouldn't give you an alternative as an answer, but looking at your comments, it looks like you're just trying to redirect.
You can use window.location.replace('http://www.google.com'); for that.
$(document).ready(function () {
window.location.replace('http://www.google.com');
});
See also, this answer.
It's not possible to trigger a click event without any user interaction. In other words, if the function being called wasn't called from a user interaction, the click event will not get triggered.
In your case, simply update the window.location attribute:
window.location = 'http://www.google.com';
try this fiddle mate, added script is
$(document).ready(function () {
//this shows up.
$('#openApp')[0].click(); //this button is not clicked.
});
I have a web page that brings up the print dialog on load. After clicking print (and focus returning to the page) I need a link to be clicked. Does anyone know a way using jQuery or Javascript that I can do this?
This is what I currently have, but it doesn't seem to be working:
<script type="text/javascript">
$(document).ready(function() {
window.print();
$("body").bind("focus", function(){ $("#logout").click() });
});
</script>
try this:
<script type="text/javascript">
$(document).ready(function() {
if (window.print())
location.href = $("#logout").attr('href');
else
location.href = $("#logout").attr('href');
});
</script>
and
<body>
<a id="logout" href="http://www.google.it">Link</a>
</body>