submitting a POST form calls my document.ready() function again? - javascript

I have an index.php page which imports a "myjs.js" file which includes a document.ready() function. Inside the document.ready() function, I show the home tab by calling .hide(); on all the divs that represent the other tabs. I also have the code so that when i click on a tab, it hides the current div and shows the div that goes with the clicked tab.
one of the tabs that I have is the profile tab, where I have a POST form with a submit button. When I click the submit button, the page goes back to the home tab (exactly the same way as when I go to the page initially). Is there a way from preventing this from happening? The submit button corresponds to some php that i have at the top of my index.php page but I don't want to leave the profile tab when I click the button :(.
Thanks!
EDIT:
actually, you can log on and see for yourselves, the code is live here: www.aaemexico.com/login.php
use credentials:
username: asdf
password: asdf
go to "perfil" tab and choose a new password (change it to asdf so that others can still access it) and click the button. preferably, i would like to not go back to the home tab after i click the button

It sounds like the form is being submitted and the page is posting. You could probably override the submit function and prevent default events to prevent the document.ready from firing again.
Or, in your document.ready, check if the page is already loaded and don't do anything if it is.

Related

Is it possible to know if the user made the call after clicking tel link?

I'm trying to make something like. When a user clicks on a tel link it will open the call screen. And after the user make the call it will redirect the user to a thank you page. By far I've done this.
$(".call").click(function(){
event.preventDefault();
window.open("tel:phone_number");
window.location.href = "thank_you_page.php";
});
By using the above codes, I almost achieved what I wanted. When the user click on the tel link it opens the call screen and also redirect them to the thank you page. But I want the user to be redirected only if the user make the call. If the user cancel the call screen or doesn't make the call, then it is not appropriate to redirect him/her to a thank you page.
Is it possible to know the user action and achieve what I described above?
And one more thing, on Desktop it opens the call alert box to a new tab with blank page.
And the blank page stays even after closing the alert box. I have to close the tab manually.
I tried window.open("tel:phone_number","_self"); but it doesn't open the Call Alert Box at all, but redirects to the thank you page.
How to make the Call Alert Box open in the same page, like it does when the tel link is used in html anchor tag <a href="tel:number"> like this?

How to trigger refresh on a page from another page (no redirect)?

So, what I have is a lot of pages like this, with GET parameters: benchmark.php?game_id=87
that display the information about the particular game (info is in a database) and also contains an Edit button.
The Edit button opens a new window using JS window.open("edit_game.php?game_id=87",...)
The Edit window contains a few textboxes to add/modify data and a Save button.
The desired behaviour here is that when I press the Save button on the edit_game.php page, not only that the information is saved in the DB (this works) but also the benchmark.php?game_id=87 page is maybe refreshed so that the information displayed is actual. I don't know how to do the 'submit on page x, page y is aware and refreshes'.
I assume I should use AJAX for this but I don't know where to start. What I tried is something like this
setInterval( function(){
$('#refresh_station').load('game_information.php');
}, 2000);
that every 2 seconds it refreshes the information present in benchmark.php?game_id=87 but I find this very inefficient since it refreshes the info even if no modifications happened.
Is this the only way to approach this situation?
Edit: I should mention that edit_game.php?game_id=87 is not supposed to close or anything after pressing Save. So I can't just use the submit form to redirect back to benchmark.php.
Yes you should use Ajax for it also add one field in Database table last_update, now when page edit_game.php?game_id=87 load it have last_update time, ajax check this last update on some interval time if ajax see there is any update page should be refreshed.
Running a loop that checks every few seconds whether the records have changed isn't the best solution in my opinion. There's a much easier way to trigger a page refresh when you submit a form in a popup window.
Using window.opener you can perform actions on the window that opened the popup that you're currently in. In your case:
<form onsubmit="window.opener.location.reload();">
Your form here.
<input type="submit" value="Save changes">
</form>
Or in jQuery:
$('form').submit(function() {
window.opener.location.reload();
});
https://jsfiddle.net/yqv1eh8w/1/
postMessage
can communicate with the child window. Link
window.addEventListener("message", function (event) {});
will let you listen to it from a child window.
This should be help you with what you want.
The entire thing communicates with the help of events and messages.
Also, you will need to run this locally (coz I opened popups to the same window)
IMO, the best approach is to use a popup form. If your web page is already using Bootstrap, you can add a modal popup easily with the edit form as it's content. When clicking on the edit button, instead of opening a new window, you can open this popup.
Then submit this form with ajax on click of the submit button and inside the Ajax success function, add javascript/jQuery to close the modal and refresh the page content.

Click button and after refresh click button from new page

Im trying to make a script for a game. Where I have to make just 2 clicks, but they are on different pages.
On page 1 I click on "Send"
javascript:document.getElementById('send').click();
When i click on that button, my page redirect to a "confirm" page. Where i need to click "Ok" button.
How can i do to click my first button and after redirect "auto click" the "Ok" button from the new page?
If you also own the second page, you can have the window.onload method check against the url, for example look for the query string ?clickthrough=true (which your first page redirects to) and if it exists simulate a click of the button.
E.g., first page redirects to /second?clickthrough=true, and your javascript on the second page checks the url like this:
if (window.location.href.split("?").slice(-1)[0] == "clickthrough=true")
// click the button
If you do not own the second page, then I'd hope there is no way to do this as it would suddenly open users to a whole host of vulnerabilities.

Inconsistency with window.history.back()

I have the following code in an MVC view that I'm using to test window.history.back()
If the user clicks the link (highlighted in red) within the paragraph tag, window.history.back() executes as I would expect. It takes me back to the prior page with state maintained on that page. However, if the user clicks the button, I don't get the same behavior. The current view is reloaded, or at least an attempt is made to reload the current page. But it fails. And it doesn't matter if the jQuery is executed, or I put the window.history.back() call within the Button onClick, the same thing happens.
A piece of information which might be helpful. The button is inside an HTML.BeginForm and the line in the paragraph tag is not.
Anyone know why this is happening?
The browser is probably interpreting the button as a submit button and submitting the form, thus causing a page refresh. Adding type="button" will prevent that.
<button type="button">Cancel</button>
$('#cancelButton').click(function(e){
e.preventDefault();
/* ... code ... */
});

Prevent browser's back button from reopening pop up window on previous page

How would I prevent the browser from opening the pop up window that was displayed from the previous page when the user clicks the back button?
I hope that made sense but I'll explain it in point form below:
There are three pages: Page1, Page2, Page3
User loads Page1 and clicks a link to load Page2 in a new window using JavaScript (i.e. window.open(...)). The User can close Page2 anytime they wish-- but we'll assume the user does so before step 2.
User now clicks a link on Page1 to load Page3.
User is now on Page3 and clicks the back button on their browser.
Page1 is displayed in the browser but Page2 is displayed again in a pop up window.
So, I'm wondering if there is a way to prevent Page2 from popping up again. I am using classic ASP as well, if it matters.
If Page2 is opened as a result of clicking a link, how is it automatically being opened when the back button is clicked? Unless Page2 is opened automatically on the initial load of Page1, it shouldn't pop up due to the back button being pressed - pressing "Back" doesn't click a link on the previous page. I suspect something else is going on in your page - can we see some code please?
you can try creating a session variable on page2. and check if it exist upon opening of page1. or use a cookie.
EDIT:
Function IsPostBack()
IsPostBack = (Request.ServerVariables("REQUEST_METHOD") = "POST")
End Function
this will not detect a page refresh though..
The page was redirecting to itself and inserting the javascript code to pop up a new window based on the button pressed previously. I think it determined this via the post or get methods. I forget now.
As such, when the user presses the back button, they will reload the page with the embedded javascript code and not the original page.
We've since left classic ASP behind so I can't really test any suggestions but appreciate the help.

Categories