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

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

Related

Can't manipulate the browser history to previous page JS

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.

JavaScript redirect just before submitting form and opening new tab

I have a situation where I need to open a new tab to an external site when the user clicks "submit" on a form, and at the same time I need to redirect the original tab to a different page to prevent the user making multiple duplicate requests to the external site.
NOTE: I have protected against this behaviour in the back-end, I just want to use JavaScript to improve the UX where possible, removing the rendering of the option in the first place.
NOTE2: This works in Firefox, but not in Chrome or Safari.
Some example code which illustrates my issue is shown below:
<script type="text/javascript">
function testFunction(){
alert("Executing testFunction()!");
window.location.replace("http://www.google.com");
}
// uncomment this line to show that testFunction() does work when called directly
//testFunction();
</script>
<html>
<head>
<title>JS Redirect Then Post Test</title>
</head>
<body>
<form action="" method="POST" target="_blank">
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname"><br><br>
<input type="submit" value="Submit" onclick="testFunction()">
</form>
</body>
</html>
When I click submit, I observe the alert popping up, but the redirect does not execute.
If I uncomment the line which calls testFunction() directly, it works as expected.
How can I get the behaviour I'm looking for?
This is what I managed to come up with after a bit of tinkering around. You can pass the click event from onclick into your handler function. If you let the event happen, it will just submit the form and prevent all following execution, that is why I stopped the original click event with preventDefault and triggered form.submit() programmatically.
Also notice how I wrapped the redirect inside a setTimeout to give time to the submit() to actually happen before the redirect.
<html>
<head>
<script type="text/javascript">
function testFunction(e) {
e.preventDefault();
e.target.parentNode.submit();
alert("Executing testFunction()!");
setTimeout(function() {
document.location.href = "http://www.google.com";
}, 0);
}
// uncomment this line to show that testFunction() does work when called directly
// testFunction();
</script>
<title>JS Redirect Then Post Test</title>
</head>
<body>
<form action="" method="POST" target="_blank">
First name: <input type="text" name="firstname"><br> Last name: <input type="text" name="lastname"><br><br>
<input type="submit" value="Submit" onclick="testFunction(event)">
</form>
</body>
</html>

Alternative for onclick window.location

I have a button:
<input class="formatButton verInfo2" type="button" value="Aceptar" id="btnAceptar" />
That when clicked calls a whole bunch of fun-ctions that in turn call PHP etc, the problem is that all this magic happens on another page, this button is a mere trigger. I wanted to load such other page when the button is clicked and onclick=window.location was doing the job, expect that when used like this:
<input class="formatButton verInfo2" type="button" value="Aceptar" id="btnAceptar" onclick="window.location='somepage.php';" />
It totally ignored the scripts that I have at the end of my document, I specially need it to trigger the last one because it uses the button's id:
<script src="js/jquery.js"></script>
<script type="text/javascript" src = "js/agregarPcLogic.js"></script>
<script src="js/addLab.js"></script>
I figure that such thing happens because it read the code in order and once the button is pressed and it loads the other page it just ignores the rest, so I figure that adding such scripts somewhere before could fix that but I think it would look bulky, so my question is, is there any other method or technique I could use to load a page?
Or is the best solution?
<input class="formatButton verInfo2" type="button" value="Aceptar" id="btnAceptar"
<script src="js/jquery.js"></script>
<script type="text/javascript" src = "js/agregarPcLogic.js"></script>
<script src="js/addLab.js"></script>
onclick="window.location='somepage.php';" />
Thanks alot in advance for your kind words of wisdom.
Decided to just add another script with:
$("id of button").click(function(){
window.location.href='link to page .php';
})

Call function in javascript file from html onclick event

This has been driving me crazy- I can't figure out why it wont work!
I have two files: myPage.html and myCode.gs in google scripts. I have deployed the html file as a web app, and I want the onclick event for the submit button to trigger the emailTech function from the myCode.gs file but it won't work! When I run the function straight from the file, it works fine.
I've done a few hours of research and tried to add <script type="text/javascript" src="myCode.gs"></script> but that causes an error when I refresh the web app. I have tried calling the function in the onClick event as onClick= "google.script.run.emailTech()" and onClick= "emailTech()" but neither work. I have also tried loading the emailTech function into the script tag in the header, but that didn't work either! What am I missing? Please help!
myPage.html file:
<script type="text/javascript"></script>
<body>
<input type="submit" onclick="emailTech();" value="Submit" />
</body>
myCode.gs file:
function doGet() {
return HtmlService.createHtmlOutputFromFile('myPage');
}
function emailTech(){
Logger.log("is this firing?");
var message = "This is the email message";
MailApp.sendEmail("XYZ#abc.com", "This is the subject", message );
}
You were actually on track with this:
<script type="text/javascript"></script>
<body>
<input type="button" onclick="emailTech();" value="Submit" />
</body>
Don't use a submit; use a button. The semantics of submits and onclick handlers are a little bizarre (not just because of HtmlService sandboxing, but even in general) and don't play well with google.script.run. This is documented in the HtmlService user guide:
" You cannot use this technique with a regular submit button"
EDIT: New answer - use google.script.run.
<script type="text/javascript"></script>
<body>
<input type="button" onclick="google.script.run.emailTech();" value="Submit" />
</body>

dojo: how to execute javascript functions of a page which is loaded by contentPane

I have following pages:
popup.html
<script type="text/javascript">
//requires
function doFunction(){
alert("hi");
}
</script>
<button dojoType="dijit.form.Button" type="button" onClick="doFunction()">Click me</button>
main.html
<script type="text/javascript">
function showPopUp(){
theContentPane.set("Href", "popup.html");
theModal.show();
}
</script>
<div dojoType="dijit.Dialog" jsId="theModal">
<div dojoType="dojox.layout.ContentPane" jsId="theContentPane">
</div>
</div>
<button dojoType="dijit.form.Button" type="button" onClick="showPopUp()">open popup</button>
when I press the button "open popup" in main.html the pop opened, and everything seems to be fine. but when I pression the button of the popup "click me" firebug shows: doFunction is not defined
is it problem of scope? how can I make the popup.html call funciones which is inside popup.html
after I post it found the answer.... the code I posted actually works, mine didn't work because I had
dijit.layout.ContentPane
instead of
dojox.layout.ContentPane
since dijit.layout.ContentPanel does instantiate dojo widgets but does not execute javascripts and dojox.layout.ContentPane does...
I don't work with dojo, but it seems like the js in the popup.html isn't initiated. Put the script tag that is in the popup.html in the main.html and it should work.

Categories