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;
Related
I want to disable the browser's back button. I have tried some javascript codes to implement the same. But those codes didn't meet my requirements. It works in firefox but not working in chrome and edge. Sometimes it works all the three but randomly working and not working.
Can anybody share me the script disable the back button in all browsers without flaws like random behaviour.
Here I showed some scripts I have tried
history.pushState(null, null, window.location.href);
history.back();
window.onpopstate = () => history.forward();
history.pushState(null, null, location.href);
history.back();
history.forward();
window.onpopstate = function () { history.go(1); };
You can refer to the following code. I test the code sample in Edge, Chrome and Firefox, it works well.
Save this file as a.html for the first page:
<!DOCTYPE html>
<html>
<head>
<title>First Page</title>
<script type="text/javascript">
function preventBack() {
window.history.forward();
}
setTimeout("preventBack()", 0);
window.onunload = function () { null };
</script>
</head>
<body>
<h3>This is first page</h3>
Goto second Page
</body>
</html>
Save this file as b.html for the second page:
<!DOCTYPE html>
<html>
<head>
<title>
Blocking Back Button using javascript
</title>
</head>
<body>
<h3>This is second page</h3>
<p>
On this page, back button functionality is disabled.
</p>
</body>
</html>
Run a.html and click to navigate to b.html. Then click browser's back button on b.html, it won't navigate to a.html.
I am using the code shown below to load a webpage into a DIV using the navigation menu. I found it elsewhere on this site on a closed post.
<html>
<head>
<title>Website</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
$(function () {
$('#mySidenav a').on('click', function (e) {
e.preventDefault();
var page = $(this).attr('href');
$('#content').load(page);
});
});
</script>
</head>
<body>
<div id="nav">
Page 1
Page 2
Page 3
</div>
<div id="content">Use the menu to navigate website. </div>
</body>
</html>
It works perfectly and is exactly what I need, but my question is can I somehow link directly to this page from somewhere else, but somehow send the pagex.html in the URL also? If I just link directly to pagex.html I don't get the navigation page around it.
You can with hash:
mydomain.com/#pagex.html
if (location.hash) {
$('#content').load(location.hash.replace('#', ''));
}
Or with querystring:
mydomain.com/?url=pagex.html
if (location.search) {
$('#content').load(location.search.replace('?url=', ''));
}
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.
});
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>
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>