href="#" onclick="closeOrCancel() and history.go(-1) in that js method doesnt work in Chrome (neither history.back())
It works with href="javascript:closeOrCancel()" , but Opera doesn't allow href="javascript:...
How to make history go back using onclick= "myFunction()" ?
Edit: closeOrCancel() returns false
Adding a return false; to the onclick code seems to be enough:
Go Back
You're wrong about two things here:
Opera allows href="javascript:...
history.go(-1) works in Chrome.
Please provide source for your script, since the problem is clearly in it and not the browsers.
Just put this in a html file and open it to see for yourself:
<script>
function goback() {
history.go(-1);
}
</script>
goback
tt
First click the "tt" link, then "goback". See the hash change. It works fine, although I'd personally recommend against using javascript in href's.
I used history.go(-2); to go back to step 1 in chrome.
Related
This code works fine in FF, it takes the user back to the previous page, but not in Chrome:
Link
What's the fix?
You should use window.history and return a false so that the href is not navigated by the browser ( the default behavior ).
Link
Use the below one, it's way better than the history.go(-1).
Go TO Previous Page
Why not get rid of the inline javascript and do something like this instead?
Inline javascript is considered bad practice as it is outdated.
Notes
Why use addEventListener?
addEventListener is the way to register an event listener as specified
in W3C DOM. Its benefits are as follows:
It allows adding more than a single handler for an event. This is
particularly useful for DHTML libraries or Mozilla extensions that
need to work well even if other libraries/extensions are used. It
gives you finer-grained control of the phase when the listener gets
activated (capturing vs. bubbling) It works on any DOM element, not
just HTML elements.
<a id="back" href="www.mypage.com"> Link </a>
document.getElementById("back").addEventListener("click", window.history.back, false);
On jsfiddle
Try this:
Link
Try this dude,
<button onclick="goBack()">Go Back 2 Pages</button>
<script>
function goBack() {
window.history.go(-2);
}
</script>
It worked for me. No problems on using javascript:history.go(-1) on Google Chrome.
To use it, ensure that you should have history on that tab.
Add javascript:history.go(-1) on the enter URL space.
It shall work for a few seconds.
javascript:history.go(-1);
was used in the older browser.IE6. For other browser compatibility try
window.history.go(-1);
where -1 represent the number of pages you want to go back (-1,-2...etc) and
return false is required to prevent default event.
For example :
Link
Use Simply this line code, there is no need to put anything in href attribute:
Go TO Previous Page
Using a link with a URL to one page and having an on-click event that overrides it is not a good idea. What if the user opens the link in a new tab?
Consider:
<button id="back">Go back</button>
<script>
document.querySelector("#back").addEvenetListener("click", e => {
history.go(-1);
});
</script>
Or if you must use a link, at least:
Go back
I'm displaying certain records in an editable table. The user when attempts to reload the table while editing a record a pop up comes warning the record about the unsaved data.
function cancelProcess()
{
if(noEditedRecords !=0)//number of edited records in the table
{
var processConfirmation = confirm("You've Edited "+ noEditedRecords +" Records. Are You sure to undo the Changes made?");
if (processConfirmation ==true){
window.onbeforeunload = null;
window.location.reload();
}
}
}
When he clicks OK to reload the page, Firefox prompts as
To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier.
And when opening the same page in Chrome, no such prompt appears.
I tried to avoid this by setting window.onbeforeunload = null;, but still the prompt window appears there.
Also I tried by changing Firefox configuration:
browser.sessionstore.postdata
Changed 0 to 1 as suggested in Mozilla support page.
But nothing worked.. How do I prevent the prompt?
Using
window.location=window.location;
Instead of
location.reload();
work for me.
I solved this, and sent data as only GET instead of POST,
It my not suit all needs but it works....
I was also using location.reload();
The behavior is correct. According to w3schools reload has a parameter forceGet that is default false, and as a result if you have a POST submit, the the browser will try to resend that POST data, and as such, it needs your confirmation. Firefox does this right, and google chrome behaves like it has a default of true for forceGet
http://www.w3schools.com/jsref/met_loc_reload.asp
while window.location=window.location or window.location=window.location.href (or any other variation of this) works most of the time, when you are using an anchor url like http://www.google.com#test will not be refreshed with this method. The browser will do the exact same thing as it will do when you are already on google.com and you change the url by adding #test. The browser will just try to locate the anchor tag in the page that has the name test, and scroll down to it, without refreshing your browser.
The solution that works in this case as well as any other case I encountered would be window.location.reload(true);
Hope this helps,
Alexandru Cosoi
With Firefox 82 (mac) I have found (big chunk of luck) the right parameter to change:
set dom.confirm_repost.testing.always_accept to true
and the confirmation request window will disapear.
window.opener.location.href = window.opener.location;
I've found the decision here
(Tested on Firefox 32 and Chrome 38 successfully.)
The way to avoid that prompt is to not create a situation where a user is trying to reload a POST result page.
Try this.
setTimeout (function () {
window.location.reload();
},0);
The solution worked for me:
-> convert the POST request to GET request.
-> location.reload() to window.location=window.location
Instead of using location.reload(), use location.href = "file_where_you_are.php";
This prevents to post twice, preventing also the message to appear.
I have a problem where an element is not showing on Opera nor Firefox. Using firebug on Firefox I could see that an error saying that a function I use to initialize the code 'is not defined'
Now here's the thing, when I empty the cache on the Firefox browser, I can see my element which contains a "facebook button" and the error is gone, but once I refresh the browser I can't see my element and returns the error again. I would have to empty out the cache in order to see the button once again.
The element is supposed to be triggered with jQuery to show if a facebook user is online, and if not it will show that button. But this is only working on Chrome & Safari, and I believe on IE8 (which i don't have) but someone told me it worked.
This is the code to show the element on my Javascript file:
jQuery('#fbLogin').show();
Now, if i was to change this on my .css file:
#fbLogin {
display:none;
}
to this:
#fbLogin {
display:block;
}
it will display, but the problem i saw was that it showed all the time, and this needs to be hidden if the user is logged in. I basically have a code that says .show the button and .hide if logged in...
here's a link to a page if you want to take a look further:
http://gullypost.com/entertainment/tim-westwood-kendrick-lamar-interview/
On Safari & Chrome you will notice that the facebook button shows up on the right sidebar, but not visible on the other browsers.
Can someone help me solve the problem to this? Thanks.
There's a weird problem when using jQuery when trying to use .show() on an element that doesn't have a defined height. The fix is to set the height either explicitly or dynamically.
$(selector).height(function() { return $(this).height() });
It looks like the second time the page loads, you're getting the following JavaScript error:
fb_og_actions_init_vid is not defined
The file that contains the function is being added to the page correctly. My only guess would be to try and move the reference to the function:
fb_og_actions_init_vid("Connected to Facebook", "108821");
To within a document.ready function:
$(document).ready(function(){
fb_og_actions_init_vid("Connected to Facebook", "108821");
});
I believe this will work:
document.getElementById('fbLogin').style.display = 'block';
I have a HTML/JavaScript project that holds a few dozen anchor tags; each of the anchor tags calls the same JavaScript function, but with a different parameter.
Everything looks good in Firefox and Chrome, but in Internet Explorer (IE) the page seems to reload (flicker) every time I click an anchor tag (like the one shown below). How can I make IE stop reloading/flickering? I would prefer not to rewrite the whole script. I have tried onclcick='javascript... and href='javascript...,but both have the same problem (although onclick seems a little better).
<a onclick='javascript:foo(22)'></a>
Try <a onclick='foo(22); return false;'></a>
Also, javascript: is pointless in event attributes as it just defines a label.
Simpler to use jQuery:
<script>
$('.action').click(function(){
yourfunction($(this).attr('rel');
return false;
});
</script>
I am having some trouble getting a function to work in internet explorer (7-8). My problem is that I need to hide some links until a user logs in, I would do this server side; however, suffice to say I have no way of doing this.
My approach has been to load a set of text (login/logout) that will change server side and test for the logout value. I was doing this by loading a div by id in Jquery and using the .text() when IE 7-8 both refuse to run this ie dev tools told me that "object doesn't support this property or method" with a reference back to the line of code containing this lookup. Code posted below:
function logintest(){
login_test= "Sign out";
alert($('#log').text());
login = $('#log').text();
if(login.search(login_test) == -1){
$('#hiddenBox').css('display','none');
}
};
The fun thing is that the alert runs properly and displays the right text string. Upon this failing I tried using the .attr() and got identical results. Any help would be great!
Jquery version: 1.4.4
Site: www.brainwellnesspro.com
IE: 7-8 on win xp
Looking at your site I'd start by fixing the html to be valid, this isn't good:
<div id="log" name="<a href='http://www.brainwellnesspro.com/login.php' onclick=''>Sign in</a> or <a href='http://www.brainwellnesspro.com/login.php?action=create_account' onclick=''>Create an account</a>">
<a href='http://www.brainwellnesspro.com/login.php' onclick=''>Sign in</a> or <a href='http://www.brainwellnesspro.com/login.php?action=create_account' onclick=''>Create an account</a>
</div>
and since you already have jquery you can check the current text value of the login link like this
function logintest(){
if($("#log a").first().text() !== "Sign out"){
$('#hiddenBox').css('display','none');
}
};
My problem is that I need to hide some
links until a user logs in, I would do
this server side; however, suffice to
say I have no way of doing this.
Why Not? Surely your application should know if the user is logged in or not (and be able to render differently accordingly?)
anyway - without seeing your markup (with the context of the element with id='log' (what type of element is this???)....
if(login.search(login_test) == -1)
should probably be
if(login.search(login_test) == '-1')