workaround to firefox creating new history after each document.write() - firefox

in Firefox after each document.write() new history entrie is made, so if document.write() is called on page load when user presses back button to go back in history firefox takes him to the current page which runs document.write() again and creates another history so even another back button pressing won't help and take him where he wants
simple workaround is:
function onbeforeunload(e)
{
setTimeout(function() { window.history.go(-2); }, 0);
}
window.addEventListener("beforeunload", onbeforeunload, true);
but then it won't let user go any link or type any address into addressbar and go there, so is there another way to fix firefox?
a better way would be to use onpopstate event instead but it does not work in firefox after document.write()
and no, document.write() must be used here.

Use document.open("text/html", "replace") instead of just document.open().
Works like a charm.
Interesting to note that other workarounds did NOT work - such as $('html').replaceWith(pageContent) or manipulating the history if the browser is Firefox...

Use document.open to run document.write while keeping the history unchanged:
<script>
document.open("text/html",document.write("foo") );
</script>
References
DOM document.open

Related

Could not show content in CKeditor after the second time

In my project, the Ckeditor is part of a webpage, which is coded using GWT. So the interface to CKeditor is using Java.
Whenever the webpage is displayed, the CKeditor will be passed a HTML via setData(), which the CKeditor is supposed to show.
The problem is: CKeditor sometimes won't show the HTML. Its content was just empty, although I am very sure the html had been passed to setData().
I had tried several approaches to solve this problem, but none would work.
My approach
Create a TextArea using DOM.createTextArea()
Call myEditor=CKEDITOR.replace(textArea, config) to initialize the CKeditor
call myEditor.setData(html) to set the content.
It was good and showed the content at the first time.
But after the webpage got hidden and shown again, I called myEditor.setData(html2) to show another html, but this time the CKeditor showed nothing.
First solution (did not work)
I changed the code to call myEditor.destroy() before the webpage was hidden, and call CKEDITOR.replace(textArea, config) again when the webpage was visible again, after that I called myEditor.setData(newHtml).
This time it worked in IE and FF, but not in Chrome and Edge, it kept showing the content of the first load time, instead of showing the new HTML.
Second solution (did not work)
I changed it to call CKEDITOR.inline(textArea, config) instead of CKEDITOR.replace(textArea, config) when the webpage was visible again, while the destroy() and setData() was still called in the same order. This time the CKeditor again didn't show anything from the second time.
Last attempt
My last approach was to not calling destroy(), but creating a new TextArea each time before showing CKeditor, and called CKEDITOR.replace(textArea, config) and setData(html). This seems to work well, but occasionally the CKeditor still showed empty content.
Could anyone please help me to solve this problem?
I am using CKeditor 4.5.8.
Finally I figured out the solution:
Before hiding the ckeditor, call editor.destroy(true), and delete editor. (Before this, I only called editor.destroy() [note: no 'true'], which didn't work well).
Here is the code:
if (editor) {
editor.destroy(true);
delete editor;
}
Use this java script code that is very simple and effective.Note editor1 is my textarea id
<script>
$(function () {
CKEDITOR.timestamp= new Date();
CKEDITOR.replace('editor1');
});
</script>
second way ! In controller ,when your query is fetch data from database then use this code after .success(function().
$http.get(url).success(function(){
CKEDITOR.replace('editor1');
});

CasperJS click event having AJAX call

I am trying to fetch data from a site by simulating events using CasperJS with phantomJS 1.7.0.
I am able to simulate normal click events and select events. But my code fails in following scenario:
When I click on button / anchor etc on remote page, the click on remote page initiates an AJAX call / JS call(depending on how that page is implemented by programmer.).
In case of JS call, my code works and I get changed data. But for clicks where is AJAX call is initiated, I do not get updated data.
For debugging, I tried to get the page source of the element container(before and after), but I see no change in code.
I tried to set wait time from 10 sec to 1 ms range, but that to does not reflect any changes in behavior.
Below is my piece of code for clicking. I am using an array of CSS Paths, which represents which element(s) to click.
/*Click on array of clickable elements using CSS Paths.*/
fn_click = function(){
casper.each(G_TAGS,function(casper, cssPath, count1)
{
casper.then ( function() {
casper.click(cssPath);
this.echo('DEBUG AFTER CLICKING -START HTML ');
//this.echo(this.getHTML("CONTAINER WHERE DETAILS CHANGE"));
this.echo('DEBUG AFTER CLICKING -START HTML');
casper.wait(5000, function()
{
casper.then(fn_getData);
}
);
});
});
};
UPDATE:
I tried to use remote-debug option from phantomJS, to debug above script.
It is not working. I am on windows. I will try to run remote debugging on Ubuntu as well.
Please help me. I would appreciate any help on this.
UPDATE:
Please have a look at following code as a sample.
https://gist.github.com/4441570
Content before click and after click are same.
I am clicking on sorting options provided under tag (votes / activity etc.).
I had the same problem today. I found this post, which put me in the direction of jQuery.
After some testing I found out that there was already a jQuery loaded on that webpage. (A pretty old version though)
Loading another jQuery on top of that broke any js calls made, so also the link that does an Ajax call.
To solve this I found http://api.jquery.com/jQuery.noConflict/
and I added the following to my code:
this.evaluate(function () { jq = $.noConflict(true) } );
Anything that was formerly assigned to $ will be restored that way. And the jQuery that you injected is now available under 'jq'.
Hope this helps you guys.

Attach event handler in chrome not working

This code:
function attachDateNavEventHandler() {
$('.ui-datepicker-title option').each(function () {
$(this).mouseup(setFlag);
});
attaches the event fine in FF but not in IE 8 or Chrome. I'm working with the jQuery datepicker and want to set a flag if the user navigates with the month or year drop-downs. I can't seem to attach to the onchange event of the selects. I think there must be an internal block on those events. I also had trouble using a simple click
Any suggestions mooooooost welcome :).
Try:
$(this).on('mouseup', setFlag);
Though this is basically the same thing you have.
I have a feeling that the options themselves may have the funny business. Options can't do everything that a typical HTML element can, but I'm not certain of the limitations on what browsers.
What about setting an on change on the whole select itself instead of trying to listen for mouseup events of each individual option.
$('.ui-datepicker-title').change(

Loading forms with .load kills the submit button in Firefox

I am currently loading several forms into a webpage with:
$(document).ready(function () {
$('#content').load('php_script.php', function() {
$(this).find('#someForm').ajaxForm(function() {
alert('Success!');
});
$(this).find('.someOtherForm').ajaxForm(function() {
alert('Success!');
});
});
});
This works in Chrome, Chromium and IE who loads the forms and everything works as it should (Clicking submit sends a request to the php-script defined in the form's action, which adds stuff to a db, and shows the alert dialog). In Firefox (v10.0.2) this code loads the forms into the DOM and displays them, but when clicking submit on any of the forms nothing happens.
At first I suspected ajaxForm, but changing the above code to:
$(document).ready(function () {
$('#content').load('php_script.php');
});
yields almost the same result, the difference being that the user is sent to the script defined as the action (Except for Firefox, where nothing happens).
How do I make Firefox not kill the submit button?
I solved it, bad HTML from my side:
<table><form ...>
<tr>...</tr>
</form></table>
Instead it should look like:
<form ...><table>
<tr>...</tr>
</table></form>
The validator did not catch this since it was loaded via jQuery (and I forgot to validate the page serving the forms), and Firefox buggered out.
The code above looks ok to me...
Have you had a look in firebug if there are any errors? Maybe there is a conflicting Id or something.
Maybe the form isnt completely loaded into the dom yet, might be worth giving live binding a try
Found this in the docs:
...jQuery uses the browser's .innerHTML property to parse the retrieved document and insert it into the current document. During this process, browsers often filter elements from the document such as , , or elements. As a result, the elements retrieved by .load() may not be exactly the same as if the document were retrieved directly by the browser...
If you inspect the form is it the same as in other browsers?

Firefox plugin that ask for some input at startup

I would like to know how to implement a dialog that show up when you first start Firefox to ask the user to enter some input. This input will be stored somewhere temporarily, and should be used later on by the plugin when required.
I have full understand of how to implement firefox plugin (this includes understanding of XUL and Javascript), so no need for full plugin example. The specific question is how to show a dialog when firefox start that ask for input, and how to store the input in a temporary storage.
Any help would be appreciated.
Add an event listener to your overlay.xul:
<window>
<script type="text/javascript">
var your_func = function (e) {
var pref = window.prompt ("Your name:","");
}
window.addEventListener ("load", your_func, false);
</script>
</window>
The your_func() will be called, whenever a new window (not a new tab) is loaded. If it should only be on start-up, you'll have to make an additional test. You find details here: developer.mozilla.org
For persistence you could store the found value as a preference: Preference Code Snippets. It would be useful then, to check in your_func, if such a preference exists, before opening the prompt.
Instead of a plain prompt, you could do the following:
window.open ("chrome://my-plugin/content/prompt.xul", "MyWindow", "chrome,modal,alwaysRaised,centerscreen");
The magic lies in the "modal" value in the third parameter.
Cheers,

Resources