I want to be able to disable history.go(-1) on certain pages.
I'm wondering if there is a way to clean up the Javascript's history object. Any ideas?
history.go() seems to not be functioning too well on certain browsers such as IE. Is that true? Any solution for that?
You can over-write the go method, but it doesn't get you much
history.go = function(){};
This will disable all uses of history.go, and not just when you pass a -1.
And as others have pointed out - this doesn't stop the user from using the Back button on their browser.
The best you can do to "disable" history is to open your page in a new window, which will have no history. Pages opened in a new window don't have a back button enabled and history.go(-1) will not do anything
Short of that, there is nothing you would (or should) be able to do.
Revised to make an implication explicit. I thought I was being clear, but apparently I was not.
You cannot override this browser behavior. If you could there would be mayhem as malware would try to take over everyone's history.
If you're trying to disable the browsers BACK button this is simply not possible. Would you like it if websites could mess around with your browsers functionality?
You've got two options.
Load pages with post request, this will prevent users from using back-button as you can trap the reload on server-side. (You need to wrap all hyperlinks with js though, so a bit of work, and the solution is quite ugly.)
Use DOM to load content into page. History won't be able to backtrack it. (or am i wrong here :-| )
describe your problem more clearly, there's probably better solutions for where you need to go.
Put this in your shared page header:
<body onLoad="history.forward()">
It will always force the user to the most recent page they've visited on your website.
This is ugly, but might work:
$(document).load(function(){
document.location.href = document.location.href + "#";
setInterval(function(){
if (window.location.hash != '#')
document.location.href = document.location.href + "#";
}, 100);
});
ugh!
Desmond,
My boss doesn't want the back button to work on the landing page.I know it's stupid but what can I do.
What about using default page, (index.php or whatever) to point forward.
index.php:
header('location:start.html');
//or likewise with js,
document.location = 'start.html';
start.html:
my real start
as user hits the back-button, the index.php will redo the forwarding.
regards,
Thanks for all the responses. After two days of juggling, I came up this solution, for now.
I use PHP to output a class to the body tag if it's the current page is the front page. Something like
<body class='front'>
and then use jQuery to enable/disable "history.go(X)" on the fly.
$(document).ready(){
if($('body.front').length>0){
$('#backbutton').click(function(){ window.history.back(); });
}else{
$('#backbutton').unbind('click');
}
}
In fact the project that I was working on require the site inside an iframe to disable the element that triggers the back button of the parent window. The js becomes:
var backbutton=parent.window.getElementById('#backbutton');
$(document).ready(){
if($('body.front').length>0){
$(backbutton).click(function(){ window.history.back(); });
}else{
$(backbutton).unbind('click');
}
}
Related
is there a way to press a button on external site with javascript and/or jquery? Like I open a new window like this:
windowObjectReference = window.open("http://some_site.html","name");
Then I want to press a button on this site. Something like this:
windowObjectReference.button.click();
Or:
name.button.click();
It would be a huge security violation if a browser would let you do that from the script placed on your own website.
So, no, this cannot be done, and should not be possible.
But...
If both sites belong to you (you have access to their code), you can pass a parameter (eg. as a hash within URL), then the target website may read it and fire the event you mentioned (name.button.click()).
You can't do this with JavaScript from a webpage.
You can do it from browser extension though.
NO !
For security reasons. This kind of attack is called clickjacking! and it was used on Twitter.
is there a way to press a button on external site with javascript
and/or jquery?
I know that I'm late at party but YES, Unfortunately it is and is a quite simple way.
make a div on your page (ex. #externalDiv);
set CSS attribute to hidden and display to none;
use (simple way) jQuery method .load() or make your own JS method using XMLHttpRequest();
load external page on your page;
click on button you wish
$('#externalDiv').load('http://www.externalPage.com', function(){$('#externalPageButtonId').click();});
Can not doge by something if you don't know how it's work :)
You are not allowed to do so because of SOP. Any trick to force user to perform click on your behalf will be considered as clickjacking attack and could lead to bad consequences.
Hi I'm wondering if there is a way to give window.history.go(-1) a default back page? If you enter a page directly from another site and someone clicks the back button, I don't want to direct them back to the referring site, I'd rather redirect them back to a search page within my application. I notice on some sites they are somehow reading the previous session, is that an option or no? Thanks
may be try something like this:
Back
I think you might be interested in the window onbeforeunload event.
Have a look at this thread: using onbeforeunload event, url change on selecting stay on this page
I have a function which upon click, it checks some information with a $.get() call before it can know if it should open a child window. It works fine, only that, because the function which opens the child window was not directly from the click event, it will be considered a pesky unwanted popup. Does anyone know of a work-around or a way to prove to the browser that it is a wanted window? Thanks.
$('#send').click(function(){
$.ajaxSetup({cache: false})
$.get('test3.html',function(data){
if(data == "processing"){
alert("still processing");
}else{
childPage = window.open("test.html","send","width = 300,height = 300");
}
});
});
It would be more reliable to pop up the window immediately (to show the user something's hapenning) with a loading screen. Then in the ajax handler you can redirect the popup to the correct page.
That said, popups in general are a bad idea. Is there no way you could do this another way?
I know that I'm not exactly answering what you want, but I found this way more appealing in general than a normal pop up, and gaining way more control.
I'm talking about using a plugin like fancybox, http://www.fancyapps.com/fancybox/#examples
witch allows you to embed iframes, or any other content you want. I'm pretty sure loading an iframe like this would be much easier than trying to sneek around browser limitations.
Does anyone know of a work-around or a way to prove to the browser that it is a wanted window
This will mean changing the browser settings, these are there to prevent what you are trying to achieve so unfortunately to my knowledge there is no way.
Instead of open a new popup, you also can try simulate a popup window via tag. Try this
Is there a way, using JavaScript or jQuery, to detect when someone goes back to your page after opening a new window or tab?
I want to create a script that opens a new window or tab, and then does something when the user comes back to the page.
Thanks,
yes, there is. Using jQuery:
$(window).bind('focusout', function(){
console.log('bye bye');
});
$(window).bind('focusin', function(){
console.log('welcome back!');
});
edit 1
Using alert() was not the best idea there :p Changed to console.log() for demonstration.
edit 2
binding to document does not work crossbrowser, changed to window
You could use cookies, but that's not flawless. If you can use a server-side language, then you can track IP addresses.
I want to prohibit the Back function of the browser.
Therefore I write the following codes in html.
<body>
<script type="text/javascript">
<!--
history.forward()
//-->
</script>
</body>
but'The above "history.forward()" did not work suddenly when
I coding html and some javascripts.
What's wrong?
In addition, please teach the different method that had a similar function.
Thanking you in advance.
Using the history object
window.history.go(positive integer)
will take you forward through history, so
window.history.go(1)
will take you forward one page.
Also, make sure you're referencing the window object.
window.history.forward()
should also work
My recommendation is to not restrict the user's browser. Web browsers have back buttons and that's the way it is. You should architect your application so that it works when the user uses the back button, such as by expiring the page and properly handling the refresh of a previous page. If disabling the back button is some attempt at security, realize that there are ways around it, especially if javascript is disabled.
If I ended up on your site and found that everytime I hit the back button the page would ignore it I would be very unhappy. You can't know that you are the first site in the browsing history.
In my opinion, having javascript functionality to control the user's history was a bad addition.