Can't manipulate the browser history to previous page JS - javascript

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.

Related

Page just reloads but doesn't redirect to the page

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.

HTML Page Being Loaded Into DIV Via Navigation - Can I Directly Link To It?

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=', ''));
}

window.history.back() not working in CSHTML page

I would like to navigate to previous page when I click back button. But when I click back button it redirects me to same page since my page has so many ajax calls.
Is there any javascript code available to navigate to previous page?
I have tried below code in my cshtml page.
<input id="btnback" type="button" value="Back" onclick="GoBack();" />
<script type="text/javascript" language="javascript">
function GoBack() {
window.history.back()
}
</script>
<script type="text/javascript" language="javascript">
function GoBack() {
window.history.go(-1)
}
</script>
You can do this in your html instead of writing function separately in scripts. See below :-
<input action="action" onclick="window.history.go(-1); return false;" type="button" value="Back" />
You can see my code in screenshot.
Go Back
I tried it and it is working. I hope this works for you as well :-)
Thanks.
Could you just add return false; to your existing javascript function and then check

I want a code page that opens 2 links from one page?

I have a link, say www.example.com/page.php. If I open this link, then how could it open www.website1.com and www.website2.com
where page.php is where I put the code.
What I want is code which will redirect to website1 and website2 when I open www.example.com/page.php.
Please give me the code for redirection.
window.location = "http://new-website.com";
window.location.href = "http://new-website.com";
window.location.assign("http://new-website.com");
window.location.replace("http://new-website.com");
I have searched some codes; will this work?
<html>
<body>
<script>
window.open('http://www.website1.com');
location.href="http://www.website2.com";
</script>
</body>
</html>
Load an html page from php script. it will open a window with one link and redirect current tab to another link.

Request Aborted on posting params using ajax

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>

Categories