refresh the previous page with history.back [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
History.Back with refresh
I have an input button that I have assigned with onclick="history.back()" .
Is there a way to use this but force the previous page to be refreshed?
I want it to work in FF and IE8

Depending on what exactly you need, here is a fix which needs some more preparation at first, but should work:
Place an invisible element with a (serverside created) timestamp in your document.
Place an eventlistenener which checks the timestamp and reloads the page if it is too old.
Now when you navigate back to this site, either via your button or the browser's back button, the onload handler will fire, check the date and reload the page if necessary.
client side pseudo code:
document.body.addEventListener("load",function(){
// compare timestamp stored in element with current time and if it is too old,
// reload page e.g. via document.location.reload()
var d1 = document.querySelector("#timestamp").innerHTML;
var d2 = Date.now;
if (d2-d1 > 10000 ) document.location.reload();
});

Related

How to redirect or load new PHP page from PHP using jquery? [duplicate]

This question already has answers here:
How do I redirect to another webpage?
(58 answers)
Closed 5 years ago.
I was trying to load new php page from one the PHP page using Jquery.
window.location = 'admin-editcp.php'; //no success
$(location).attr('href','admin-editcp.php'); // no success
Checked my browser JS is also enabled. I tried it with Mozila, IE and Chrome.
Then i manage site in dreamweaver and I checked it there via live preview and when i clicked in click event, it loads properly.
But not in the above two methods with 3 browser.
I am confuse that what is missing here????
What I have tried:
You can use 'location.href' for this
location.href = "admin-editcp.php";
You can use this :
jQuery('#id').on('click', function(){
// Perform your action on click here, like redirecting to a sign-up url
window.location='https://www.requiredpageurl.php';
});
You're allmost correct. It should be:
window.location.href = "admin-editcp.php";

Modify div after it's created [duplicate]

This question already has answers here:
Detect changes in the DOM
(11 answers)
Closed 7 years ago.
I've a php page with a menu.
One of the items of the menu, invokes another php, which returns a div element filled with data, that is inserted into the former page (that the user is already seeing).
I'm trying to modify this div content using JavaScript, after loading it. However, I can't achieve it.
I've tried using ready() method, but since it happens at a random time after loading it, this method doesn't work.
I've also tried with a load() method I found which seemed to trigger after the item is loaded completely, but I didn't complete it neither. This was something like:
$( "#overlay #overlay_text div #panes div #tableInOverlay" ).load(function() {
alert("The table has been loaded");
});
How can I achieve it?
Thank you in advance
Edit: in order to try to make it clear
User access to the web page (File 1) -> User clicks in (File 1) Menu -> File 2 is invoked, which creates a response with a div element -> (File 1) gets the response, and insert it into the HTML code, so the new div is shown.
Then, at this moment, I need (File 1) to modify this div that it just received.
There is no html event that is triggered when elements are inserted, modified or removed.
Possible solutions:
1- Trigger an even yourself in the callback:
$('#myDiv').load('url', function(){
$('#myDiv').trigger('foohappened')
});
$('#myDiv').on('foohappened', function(){
// modify content
});
NOTE: myDiv is an element on the page, not the one that loads via ajax
2- Change the data before inserting
$.ajax({
success: function(data){
var loadedElement = $.parseHTML(data);
var modifiedContent = modifyContent(loadedElements);
$('#myDiv').insert(modifiedContent);
}
})
Can you try this?
$(window).load( function() {
// YOUR CODE HERE
});
also, if you know the amount of the delay, you could postpone your function using setTimeout()

Click Anywhere Pop Up Windows Once pervisit?

Anyone can help how to open new windows when clicked anywhere but only once pervisit.
Example, you visit google.com and click anywhere there, and new window will open but only once, second click will not open windows.
Site example : http://afowles.blogspot.com
My script is
Function popup() {
window.open ("http://www.stackoverflow.com","popup","menubar=1,resizable=1,width=450,height=550");
And then put onclick="popup()"
In body.
Code above show new windows every single click on the site. What should i do to make it only show once?
Set a global variable
a=1;
then in function check for variable's value. After windows.open is executed, change the global variable's value, so that windows.open will not be executed again.
Code:
<script>
var a=0;
function popup() {
if(a==0){
window.open ("http://www.stackoverflow.com","popup","menubar=1,resizable=1,width=450,height=550");
a++;
}
}
</script>
Firstly you will need to define to yourself what a visit consists of, i.e. once per day, week, or each time a users lands on your site, every page loaded, etc.
Assuming you want some persistence for your users while they browse, what you need to do on your site is set a condition to be evaluated prior to the first click and if the result indicates a users' first visit then open the page. At the same time, set a value to be checked next time (and most likely all subsequent clicks based on your current implementation). LocalStorage or cookies would be your best options for this.
I would set up something along the lines of:
//check page is loaded
if (cookie.firstVisit) {
//add click event listener
} else {
//set cookie.firstVisit to false
//remove click event listener
}
Instead of spelling out how to do this with cookies here, have a look at this article which explains it all: How to set/unset cookie with jQuery?
Lastly, opening a new window when someone clicks anywhere on your page without them explicitly wanting that action perform is considered bad practice in most scenarios. However, I do not know your situation so this may be the best course of action but thought it was worth mentioning.

reload to top of page [duplicate]

This question already has answers here:
How to scroll to top of page with JavaScript/jQuery?
(28 answers)
Closed 8 years ago.
SOLVED
add this to the html:
<body onload="location.href='#menu'">
Its the only solution and not a duplicate of the scroll question....pff.
I have edited this question in hopes that the SO community would revisit it. None of the solutions in the supposed duplicate question work or I am using them incorrectly. I posted my code with two of the solutions to show that I am trying. Please help!
Super basic javaScript question I can't find.....I'm learning from trial, mostly error, and StackOverflow.
When I refresh I want to return to the top of the page (F5 or browser reload or any other way the page can be reset). I believe this was the 'old' way browsers worked but when I refresh I stay in the same place on the page. I'm testing in Chrome. Of course I would like this behavior in all browsers.
In this case I am building a single page app. Most mobile users will never use the refresh/reload as it is buried in the hamburger, but I would like to handle this event by going back to the home state. In this case the <div data-role="page" id="menu">.
I am using jquery but I want to make sure and learn javascript.
I thank you all.
code:
document.ready = init;
function init(){
var button = $('#facultyButton')
,facList = $('#facultyList')
,search = $('#facSearch')
,monikerBox = $('.monikerBox')
,events = $('#events')
,ajaxString = "http://stage.webscope.com/veith/dev/ajax.pl?"
,html = "";
//executed on init
console.log('page init');
$('html').scrollTop(0);
//events
$(window).on('load',function(){
$('html, body').scrollTop(0);
});
button.click(function(){
var value = "a";
The window's load event only runs once right after the page is done loading. So this way you can be sure that after the page loads no matter what reason (first view or refresh) it will run.
$(window).on('load', function(){
$('html, body').scrollTop(0);
});

Call a JavaScript Function when Back Button of Browser is Clicked [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Best way to detect when user leaves a web page
How can I call a javascript or jQuery function when back button of browser is clicked?
I have seen some solutions but none of them worked for me.
Can anybody show me some example code?
In jQuery try this :
$(window).unload( function ()
{
// put your code here
});
In javascript :
window.onbeforeunload = function () {
// put your code here
}
<body onUnload="yourFunction();>
<!-- ... -->
</body>
You can use history.replaceState() (see browser support) to modify the previous URL you visited to the current page with an added parameter of your choosing like for instance redirect=<URL of previous actual previous page> (you can maybe get this URL from the referer?), then detect the parameter in document ready and perform the required actions.
This is just an untested theory, I might elaborate later on when I get to test it.

Categories

Resources